Web Programming TutorialsLearn About The Express Framework In Node.Js

Learn About The Express Framework In Node.Js

Express Framework

In this article, we are going to discuss Express framework, which is a marginal and flexible Node.js web application framework. It provides a robust set of features which help to build several web and mobile applications. It enables the rapid development of any Node based Web applications. The following are some of the core features which are offered by the Express framework.

• It permits to set up middlewares in order to respond to the HTTP Requests.
• It defines a routing table, which can be used to achieve different actions based on the HTTP Method and URL.
• It permits to dynamically render the HTML Pages which is completely based on passing arguments to the templates.

How to install Express framework?
Before we can install express framework, we should make sure that Node.js is installed on our machine. We can use NPM to install the Express framework globally which will make sure to create a web application using the node terminal. Simply execute the below command to complete the installation of the express framework.

C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>npm install express --save
[email protected] C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication
`-- [email protected]
  +-- [email protected]
  | `-- [email protected]
  |   `-- [email protected]
  +-- [email protected]
  +-- [email protected]
  | `-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  | `-- [email protected]
  +-- [email protected]
  +-- [email protected]
  | `-- [email protected]
  |   `-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  `-- [email protected]

npm WARN [email protected] No repository field.
C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>

The execution of the above command will save the installation locally in the node_modules directory. It will create a directory express inside node_modules directory. We should also install the following important modules along with express framework modules.

• The body-parser module: It is a node.js middleware which helps to handle JSON, Raw, Text and URL encoded form data.
• The cookie-parser module: It helps to parse the Cookie header and populate req.cookies with an object keyed by the cookie names.
• The multer module: It is a node.js middleware which helps to handle multipart/form-data.

Execute the following commands to execute these three modules.

npm install body-parser --save
npm install cookie-parser --save
npm install multer --save

The execution of the above three command will save the installation locally in the node_modules directory. It will create a directory body-parser, cookie-parser and multer inside node_modules directory. On command line, the following will be the execution log for these three commands.

C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>npm install body-parser --save
[email protected] C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication
`-- [email protected]
  +-- [email protected]
  +-- [email protected]
  `-- [email protected]

npm WARN [email protected] No repository field.

C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>npm install cookie-parser --save
[email protected] C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication
`-- [email protected]

npm WARN [email protected] No repository field.

C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>npm install multer --save
[email protected] C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication
`-- [email protected]
  +-- [email protected]
  +-- [email protected]
  | +-- [email protected]
  | | `-- [email protected]
  | `-- [email protected]
  |   +-- [email protected]
  |   +-- [email protected]
  |   `-- [email protected]
  +-- [email protected]
  | +-- [email protected]
  | | +-- [email protected]
  | | +-- [email protected]
  | | +-- [email protected]
  | | +-- [email protected]
  | | `-- [email protected]
  | `-- [email protected]
  +-- [email protected]
  | `-- [email protected]
  +-- [email protected]
  `-- [email protected]
npm WARN [email protected] No repository field.
C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>

Example with the Express Framework
The following is a very basic example on the use of express framework. Here, we are creating a JavaScript file ‘express-module-demo.js’ which has the following lines of code.

var express = require('express');
var demo_app = express();
demo_app.get ('/', function (req, res) {
   res.send ('Welcome to the express module!');
})
var server = demo_app.listen (8088, function () {
   var host = '127.0.0.1';
   var port = server.address().port
   console.log("Application is listening at http://%s:%s", host, port)
})

Output: – Execute the below command on the command line in order to run above program.

C:\odesk\Abhishek Thakur\NodeJS\MyNPMApplication>node express-module-demo.js
Application is listening at 

In the above example, we have used the express module to create a web application that listens to a specific URL [http://127.0.0.1:8088/] and response back the website with string as ‘Welcome to the express module!’ as shown below.
Express Module
Explanation of above code
1. Request & Response: – In the express application, we use a callback function which has two parameters known as request and response objects as shown below.

demo_app.get ('/', function (req, res) {
   res.send ('Welcome to the express module!');
})

• The request object is nothing but the HTTP request which has properties for the request query string, parameters, body, HTTP headers, etc.
• The response object is nothing but the HTTP response which an Express application directs when it receives an HTTP request.
• We can print request and response objects for their HTTP request and response related information such as cookies, sessions, URL, etc.

2. Basic Routing: –Routing denotes to evaluate how an application responds to an incoming client request to a particular endpoint. The endpoint is a URL or path which has a specific HTTP request method such as GET, POST, etc.

demo_app.get ('/', function (req, res) {
   res.send ('Welcome to the express module!');
})

Conclusion: –
In this article, we discussed the installation and use of the express framework in building a basic web application with a suitable example.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -