30 Days of node
Day 11 : Express Framework






30 days of node - Nodejs tutorial series
What is express.js ?

Express.js or simply express is a minimal node.js web apps framework which is flexible and provides a set of features such as simple APIs used for building backend of websites and mobile applications. Express is open-source and currently maintained by node.js foundation. Express also provides us with very basic middleware and declarative routing.
Advantages of using express web application framework includes :

  1. It makes routing pretty easy.
  2. As it is built on top of node.js, so fast I/O.

Prerequisites

Prerequisites for installing express are :

  1. Node.js - we can check it using the following command on terminal :
    											
    >node -v
    v8.5.0
    											
    										

  2. NPM - we can check it using the following command on terminal :
    											
    >npm -v
    5.4.2
    											
    										

Installing express

We can install express in the following ways :

  1. Installing express globally : We can install express with global scope in the following way
    											
    >npm install express -g
    											
    										

  2. Installing express locally : We can install express with local scope in the following way
    											
    >npm install express 
    											
    										

  3. Installing express with --save flag : We can install express with --save flag to add dependency in the package.json file.
    											
    >npm install express --save
    
    											
    										

Simple server in express.js

Let's create a simple server in express which will send hello world as the response to the user.

											
var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})
app.listen(3000, function () {
  console.log('Server is listening at 3000')
})
											
										

We can run the above code using the following command :
											
>node server.js
Server is listening at 3000
											
										

Now open http://127.0.0.1:3000 in your browser.

Routing in express.js

One of the biggest advantage of using express is that it makes routing very simple. An example which explains routes in a very simple way is given below :

											
var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Simple Example of routes!');
})

app.get('/signup', function (req, res) {
  res.send('This is demo route for sign up');
})

app.get('/signin', function (req, res) {
  res.send('This is demo route for sign in');
})

app.get('/signin/dashboard', function (req, res) {
  res.send('This is demo route for user who signed in and now reaches their dashboard');
})


app.listen(3000, function () {
  console.log('Server is listening at 3000')
})
											
										

We can run the above code using the following command :
											
>node routes.js
Server is listening at 3000
											
										

    We can open any of the following in our browser :
  • Now open http://127.0.0.1:3000 in your browser for default route
  • http://127.0.0.1:3000/signup for demo signup route.
  • http://127.0.0.1:3000/signin for demo signin route
  • http://127.0.0.1:3000/signin/dashboard for demo dashboard route.

Request with parameters in express.js

In order to understand how the request with parameters is handled in express let's take an example : Suppose we have a sign up module in which we are receiving 3 parameters from the front-end which are name, email and password. ( Normally, we store data in a database . we will perform that in the coming chapter. For now , just focus on understanding just the request with parameters part)

											
var express = require('express');
var fs = require('fs')

var app = express()

app.get('/', function (req, res) {
  res.send('Simple Example of routes!');
}) 

app.get('/signup', function(req,res){
	// this is how we will receive params from front end 
	
	var name = req.query.name; 
  var email = req.query.email;
  var password = req.query.password;
  //For demo purpose
  console.log(name + '' + email + ' ' + password);

  /**
   * Store this in a database and perform further processing
   */
    res.send("In signup module")
});

app.listen(3000, function () {
  console.log('Server is listening at 3000')
})
											
										

we can check this by running this code in the following way :
											
>node routes-with-params.js
Server is listening at 3000
											
										

You can perform the POC using google chrome's plugin postman .

Summary

In this chapter of 30 days of node tutorial series, we learned about what is express.js, how to install express , simple server to serve hello world using express , basic about routing and how to handle request params.