Developing a REST service quickly using node js and express 4

Recent times , we develop applications, which are totally separated as Server side applications and client side applications.
Here , Client side application can be anything like android, html app, or any other REST service consumers.

When I was working on one such client side applications, I wanted to test how the application is able to consume a REST service. I want a server side application which simply returns a json response, so that I can test it with my app.

I just want it quickly and i just need it for a test. So I decided to use nodeJs and express js to develop such a REST service almost in 15 minutes of time. 🙂

All I did was described as steps below:

1. Create a folder , enter to that folder then run
npm init
It will ask you a few details such as name , version etc, just follow the flow and enter.

2. It will create packages.json file as follows:
{
"name": "myserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "mksiva",
"license": "ISC"
}

3. Now I am going to install two important modules

  1. express js
  2. body-parser

Express JS
Express is a light-weight web application framework to help organize your web application into an MVC architecture on the server side. You can use a variety of choices for your templating language (like EJS, Jade, and Dust.js).

You can then use a database like MongoDB with Mongoose (for modeling) to provide a backend for your Node.js application. Express.js basically helps you manage everything, from routes, to handling requests and views.

Body parser
Body-parser extract the entire body portion of an incoming request stream and exposes it on req.body as something easier to interface with . I hear, yes! you can do it by yourself as well but using body-parser will do what is required and will save your trouble.

I am installing this as we might want to test some POST rest service.

Run the following commands:

1. npm install express --save
2. npm install body-parser --save

This will add express js and body-parser dependencies in packages.json file
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2"
}

4. Now our application structure is ready, we will develop some small server which returns a json file when we call ‘/api’.
1. Create a file called server.js
//Import require modules
var express = require('express');
var bodyParser = require('body-parser');

// define our app using express
var app = express();

// this will help us to read POST data.
app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());

var port = process.env.PORT || 8080;

// instance of express Router
var router = express.Router();

// test route to make sure , it works.
router.get('/', function(req, res) {
res.json({ message: 'Hello! welcome to our rest api!' });
});

// all our routes will be prefixed with /api
app.use('/api', router);

// state the server
app.listen(port);

console.log('Server listening on port ' + port);

It is done now, run node server.js

It will print “Server listening on port 8080” and it is running now.

If you hit http://localhost:8080/api

You will see:

Thats it. You can test your the simple REST service now.

If you want to know the basics of node.js you can check the following blog: Node js Getting started.

Leave a Reply

Your email address will not be published. Required fields are marked *