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 :
Prerequisites for installing express are :
>node -v
v8.5.0
>npm -v
5.4.2
We can install express in the following ways :
>npm install express -g
>npm install express
--save
flag : We can install express with --save flag to add dependency in the
package.json
file.
>npm install express --save
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')
})
>node server.js
Server is listening at 3000
http://127.0.0.1:3000
in your browser.
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')
})
>node routes.js
Server is listening at 3000
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.
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')
})
>node routes-with-params.js
Server is listening at 3000
postman
.
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.