Express Framework in Node.js

Anup Sarkar
Tensult Blogs
Published in
3 min readApr 17, 2018

The most important topic in Node.js is Express Framework.

What is Express?

Express is a node.js server framework to build a web application. It contains a middleware framework. It is the standard server framework for node.js.

Express framework makes it easy to build a web application. It can also handle multiple types of requests like GET, PUT, POST and DELETE.

Before installing express, we need to complete installing node.js on the system. Once done, you can follow the below steps to install express.

npm install express --save

After installing the express if you want to work with express, you just need to include the express module.

const express= require('express');

What are the benefits of Express over Normal Node.js?

  1. It is used for easier creation of web applications and services.
  2. Consistent Routing to modules.
  3. Consistent Middleware interface.
  4. Handling post data, session management, static file serving, etc.

To create a simple express example on your IDE, you will have to create a directory where you want to save the below code.

mkdir newApp   //Make a directory to save the codecd newApp/  //Enter into the created directory

After creating the directory, change the directory through command line as described in the previous step. Then create a package.json file by using npm command to store app configuration and to enable your node modules. Just write the following in your command line.

npm init  // Create a package.json file in your directory

Install express in your local directory.

npm install --save express

Example:

Create server.js:

const express = require('express');let app = express();//For Responding GET request
app.get('/', function (req, res){
res.send('Response recorded for Homepage');
});
app.listen(3000, function () {
console.log('app is running on port 3000');
});

To run the code do the following :

node server.js

For testing the output on the browser, type the following in the URL bar

localhost:3000/   //For checking Output

Router Module in Express :

This module is useful for creating separate route of a particular application. By using express we can create the Router instance for routing the application.

Create route.js in your root directory:

const express = require('express');
const router = express.Router();
// define the home page route
router.get('/', function (req, res) {
res.send('This is the Home Page');
});
// define the contact route
router.get('/contact', function (req, res) {
res.send('Contact Page Information');
});
module.exports = router;

Create another file named app.js, which would be the starting file of your application:

const express = require('express');let app = express();const route  = require('./route');app.use('/', route);app.listen(3000, function () {
console.log('app is running on port 3000');
});

Run the application using the following command. You will get your output as defined in the route.js .

node app.js       //To start your application

Visit the following urls on your browser:

localhost:3000/             //It will show the output of home pagelocalhost:3000/contact     //It will show the contact page info.

Why we need body-parser ?

To handle HTTP POST request in Express.js, we need to install middleware module called body-parser.

To know more about body-parser, refer the npm body-parser Readme section.

npm install body-parser --save  //To installed body-parser

Body Parser Implementation :

const express = require('express'),
app = express(),
bodyParser = require('body-parser');

// support parsing of application/json type post data
app.use(bodyParser.json());

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));

I hope this will help you get an idea about how Express works.

--

--