Learn About The Express Framework In Node.Js

0
2401
Express Framework

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.

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.

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.

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.

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

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.

• 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.

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