Create a new Node.js Project

Summary

Here is a quick tip for quickly installing Node for mocking purposes.

Update Node and NPM.

npm install node@latest -g
npm install npm@latest -g

Initialise a new Node.js project.

npm init
{
  "name": "azureservicebusmicroservices",
  "version": "1.0.0",
  "description": "Microservice built with Azure Service Bus and Node.js",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "azure",
    "service",
    "bus",
    "microservice"
  ],
  "author": "Syed Hussain",
  "license": "MIT"
}

Install live-reloading module (Nodemon). The -g flag installs the module globally for all future Node.js projects. If you have already installed this globally, omit the -g flag.

npm install -g nodemon

Install Express

npm install express

Add the –save modifier to update the project

npm install express --save

Install additional packages:

body-parser – middleware – allows the capture of input as a request and provides the ability to parse this.

Cors – package to enable the handling of cors

npm install body-parser cors --save

Create the Server

Update the package.json file removing the ‘test’ and replacing with the following lines (start and dev).

{
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js"
  }
}

Note: To run in DEV mode, run the server using the following command:

npm run dev server.js

Leave a comment