6

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

 3 years ago
source link: https://blog.knoldus.com/create-deploy-microservices-nodejs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Reading Time: 2 minutes

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.

microservices

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: mkdirnodejs-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:

xxxxxxxxxx
module.exports = (req, res) => { res.end('Welcome to micro') }

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:

xxxxxxxxxx
function randomNumber (){
return Math.random();
}
module.exports = (req, res) => {
const number = randomNumber();
return {
num: number
}
}

now run : localhost:3000 and you will see something like this in browser as API response.

xxxxxxxxxx
{"num":0.4303221087853306}

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 !!



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK