Creating Express Web Server

Express server came handy and it deeps through many user and community. It is getting popular.

Lets create a Express Server. For Package Management and Flexibility for Dependency We will use NPM(Node Package Manager).

  1. Go to the Project directory and create package.json file.
***package.json***
{

“name”: “expressRouter”, “version”: “0.0.1”, “scripts”: { “start”: “node Server.js” }, “dependencies”: { “express”: “^4.12.3” } } 2. Save the file and install the express dependency using following command npm install. This will create node_modules in you project directory along with required dependency. 3. Let’s create Express Web Server. Go to the Project directory and create server.js file. server.js

var express = require("express");
var app = express();

//Creating Router() object

var router = express.Router();

// Provide all routes here, this is for Home page.

router.get("/",function(req,res){
res.json({"message" : "Hello World"});

});

app.use(”/api”,router);

// Listen to this Port

app.listen(3000,function(){ console.log(“Live at Port 3000”); });

For more detail on setting node server you can see [here][1].
  1. Run the server by typing following command.
node server.js

If Server runs successfully, you will se something like this.[![this][1]][1].
  1. Now go to the browser or postman and made a request
<http://localhost:3000/api/>

The output will be [![this][2]][2].