Create & Deploy a microservice in less than 5 min: NodeJs

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.

Features:

  • Easy. Designed for use with async and await
  • 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 async transpilation 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:

“start”: “micro”

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

https://microservice-wfpwfyqrfq.now.sh

So this is not all, try creating other APIs and enjoy microservices.

This post is in reference with Knoldus FrontEnd Initiative: here

Thanks !!


KNOLDUS-advt-sticker

Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

Leave a Reply

%d bloggers like this: