Express Framework in Node JS

 

Express Framework
     Previously we saw how to build a basic web server using http module of NodeJS. But there were some drawbacks regarding code maintainability. Hence a library/module for NodeJS was developed -- ExpressJS, which can handle the routes and functions of your server. 

As we said, ExpressJS is a NodeJS module which is not a core NodeJS module. This means we need install express for our application.

Install ExpressJS by  running “ npm install express --save ” in the terminal in your project directory. That is all we need to do. 

Now we will create the same server we previously built using ExpressJS.
Change your server.js file to:

server.js

"use strict";

const express = require('express');
var app = express();
let PORT = 3000;

app.get('/articles', function(req, res){
    res.end("Code to display articles here");
});

app.get('/stats', function(req, res){
  res.end("Stats will be displayed here once we complete our app");
});

app.use(function(req, res){
  res.end("More logic to be implemented");
});

app.listen( PORT, function(){
    console.log("Server started listening on port: "+PORT);
});

Here we require the express module and create a new instance of express which will be our server. With this server, we specify the handler for a url. 

All the assigning of handlers to the routes should be called before app.listen(). The order in which also matters as the routes are matched from top to bottom. The handler that matches the route first is executed independent of the next handlers.

Restart your server and open your web browser and visit the previous urls. You should get the same responses as before.



As with the http module, the handler function contain req, res and a next (optional) arguments which you can use to achieve various functions. The req object stores information about the request like the url, params body etc while the res object stores info about the response. 

Different request methods
     In the previous example we saw about send a GET request. Now we need to send some POST request. In a POST request the body is sent as a string. For the ease of processing we will parse it into JSON object. For parsing we will use another module - body-parser. Install the modules using

> npm install body-parser --save                                                               

Now we need to tell the Express App to use this module. So in the server.js file, before we declare the routes add the following lines

let PORT = 3000;

// Add this code
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Till here

app.get('/articles', function(req, res){
...

For testing our POST request we will use an app POSTMAN ( you can download it from https://www.getpostman.com/) .

Add an handler to accept a post request:


app.post('/post-request', function(req, res){
  res.json(req.body);
});

Once we use body-parser module, the req.body object in the handlers will contain the body of the POST request. In this handler we are send the body of request as the response in JSON form.

 Restart the server and send a post request:



Hit send and you should see a response like:



You can send other requests like PUT, DELETE etc apart from GET and POST using Postman and handle it using Express Handlers


No comments:
Write comments

Popular Posts