AMAZON CONTENT

Create Sample Node Micro-Service


Create Sample Node Micro-Service


Hello there !!! Hope you are doing good.
Today, in this article we are going to show you how you can create sample Node Micro-Service.

In this article we will create one GET API which will response that API health check is success or not ! Follow below steps to create micro-service.

Step 1: Create directory (e.g. nodeMicroservice)
mkdir nodeMicroservice

Step 2: Type following command to generate package.json
npm init

Append --yes to above command if you don't want to add package file content manually.

Step 3: Create app.js file
touch app.js

Step 4: Write down below code into app.js
// using express node-module and creating it's object
const express = require('express')()

// GET API which is used for testing purpose
express.get('/', (req, res) => {
    return res.send({
        "msg": "Service Health Check Success!!!"
    })
})

// Setting port, on which micro service will respond
// This is required, if you are not adding below line then your command node app.js
// will terminate your js execution
express.listen(3000)

// exporting module
module.exports = express

Step 5: Open cmd at created folder and run app.js file
node app.js

Step 6: Open up your browser or Postman to check API.Here I am attaching cURL script, which you can run into Postman.
curl -X GET \
  http://localhost:3000 \
  -H 'cache-control: no-cache'

Hurreyyy ... ! Hope you will get below JSON response. (If you are facing issue then comment in this post, we will solve together)

{
    "msg": "Service Health Check Success!!!"
}

Thanks ...  🙌

Post a Comment

0 Comments