Framework: It is known to be a skeleton where the application defines the content of the operation by filling out the skeleton. For Web development, there is python with Django, java with spring, and For Web development in we have Node.js with Express.js in node.js there is an HTTP module by which we can create an only limited operatable website or web application. In general, the real working of any web application or website is that it is capable to handle any kind of request. Requests may be post, get, delete, and many more like a request for an image, video, etc that’s why Express.js is used as a Framework for Node.js.
Express.js is a routing and Middleware framework for handling the different routing of the webpage and it works between the request and response cycle.
Working of Middleware Framework:
There are lots of middleware functions in Express.js like Express.js app.use() Function etc.
Parameters: It accepts the two parameters as mentioned above and described below:
- path: It is the path for which the middleware function is being called. It can be a string representing a path or path pattern or a regular expression pattern to match the paths.
- callback: It is the callback function that contains the request object, response object, and next() function to call the next middleware function if the response of current middleware is not terminated. In the second parameter, we can also pass the function name of the middleware.
The working cycle of multiple Middleware:
Benefits of using Express.js Middleware:
1. We generally use http.createServer() to create a server and performs request and response according to the information, but we cannot check what type of request made by the client so that we can perform operations according to the request.
2. Express.js contains multiple methods to handle all types of requests rather than work on a single type of request as shown below:
- Express.js req.get() Method: This method is used when get request is done by the client for eg: Redirecting another webpage requests etc
- Express.js req.post() Method: This method is used when post requests are done by the client for eg uploading documents etc.
- Express.js req.delete() Method: This method is used when a delete request is done by the client it is mainly done by the admin end for e.g. deleting the records from the server.
- Express.js req.put() Method: This method is used when update requests are done by the client to update the information over the website.
3. Easy to connect with databases such as MongoDB, MySQL.
4. Easy to serve static files and resources we can easily serve HTML documents using express.js.
5. There are several other benefits of using Express.js like handling multiple get requests on a single webpage that means Allows you to define multiple routes of your application based on HTTP methods and URLs.
Install the express module using the following command:
npm install express
Run the index.js file using the following command:
Now open your browser and go to http://localhost:3000/GFG, you can see the following output:
Now go to http://localhost:3000/hello you can see the following output:
Note: Handling Multiple requests using the HTTP module by default is a get request. This method cannot be used for multiple handling requests. If we use the HTTP module for handling multiple get requests it requires more length of code and multiple if-else conditions to handle the different routes.
Handling Multiple requests using an HTTP module:
Calling multiple middleware from single middleware:
Now open your browser, you will see the following output:
The following will be the output on your terminal screen:
Sending HTML documents using Express.js:
The express.static() middleware is the of the express.js module is used for serving the HTML static documents. The benefit of using it automatically fetches the name of the HTML document present in the particular directory.
Run the app.js file using the following command: