These days projects & developers are happily using microservices because the idea behind microservices is that some types of application become easier to build and maintain when they are broken into smaller and composable pieces which work together, whether we talk about Angular 2 or React or some backend frameworks they are now component based and each component is developed separately if we think about larger project size, the reasons are proven for using this approach for enterprise projects.
You already heard about resilience so rather than relying upon a single virtual or physical machine, components can be spread across multiple servers or even multiple data centers. If one component dies, you spin up another, and the rest of the application can continue to function.
Shut up and start implementing it: So there are predefined ways to create APIs in nodejs and it takes 2 minutes to setup a server and implements an API, but today we playing with micro – a npm package provided by zeit.
- Easy. Designed for use with
- Fast. Ultra-high performance (even JSON parsing is opt-in).
- Micro. The whole project is ~100 lines of code.
- Agile. Super easy deployment and containerization.
- Simple. Oriented for single purpose modules (function).
- Explicit. No middleware. Modules declare all dependencies.
- Standard. Just HTTP!
- Lightweight. The package is small and the
asynctranspilation is fast and transparent
Steps: goto terminal type: mkdir nodejs-microservice, cd into it and type in terminal again, npm init -y
type npm install –save micro in terminal, this will install micro package, now open project folder in any editor. go to package.json file and add start script as:
main file is “index.js” so create it in editor and write this code in:
now run: npm start and here we go B-) your server is running at localhost:3000
Now you can add any api and its corresponding functionality in nodejs, so lets create a random number generator api.
so in index.js:
now run : localhost:3000 and you will see something like this in browser as API response.
Now this service is working locally but we have to deploy it: so run this command:
npm install -g now
now in a terminal inside project directory: $now
now follow some magical steps and your service is live now 😉 check mine it’s here
So this is not all, try creating other APIs and enjoy microservices.
This post is in reference with Knoldus FrontEnd Initiative: here