Build and Run Node.js Microservices with Zero Code (Well, Sort Of)
Get a look at this handy module that can help you quickly set up microservices with a minimal amount of code.
Join the DZone community and get the full member experience.Join For Free
Today, I'm introducing the npm module `microservice-bootstrap.`
It started as a hobby side project and was basically a scaffolding app for building API endpoints on top of expressJS. I've since added a whole suite of command line tools and made it as simple as it gets. See below how easy it is to create a new service in a few minutes (well, depending on how fast your Internet connection is).
This goes without saying, but you need npm. If you don't have it, Google it.
Then you want to install the command line tool, simply type in your terminal window:
npm install -g microservice-bootstrap
Create a new Project
Now go ahead and create a microservices project, say, 'my-awesome-api'. All you need to do is find a good location for it (let's use /tmp as this is an example), then type in:
microservice-bootstrap create -t /tmp/my-awesome-api
Wait for it to finish, and voila! There you have it.
Add new Service Modules
Note how I mentioned 'microservices' project with a plural 'services'? Yep, you can add as many services as you like, and you can choose which ones to run, so the idea is one code base with infinite possibilities. DevOps will thank you for that.
To add a new module, simply go to your project root directory (/tmp/my-awesome-api), and type in:
microservice-bootstrap add-service service1
And yes, you can add multiple ones in one go as well:
microservice-bootstrap add-service service1,service2,...
This will add services with clean folder structure, and an example endpoint (GET /service-name), e.g. if you added 'user' module, it will enable GET /user endpoint.
Running the Service(s)
Running is no sweat, again, no code, just CLI...
microservice-bootstrap run -e local -s service1 -v -p 8080
And yeah, you would have guessed, it supports multiple modules, just type:
microservice-bootstrap run -e local -s service1,service2 -v -p 8080
See it in Action
Check this out — service1 endpoint, freshly added with zero code change:
And, with a built-in debug (well non-production only, of course), you can get extra debug info right there:
There you have it, happy servicing (and coding)!
By the way, it comes with Cluster built in — it will automatically scale up to the number of processors on the server.
Of course, with each command, adding -h will show you the man page. Go explore!
Published at DZone with permission of Bruce Beibei Li. See the original article here.
Opinions expressed by DZone contributors are their own.