![]() |
VOOZH | about |
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 only limited operatable websites or web applications. 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 posted, 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.
Syntax:
app.use(path,(req,res,next))
Parameters: It accepts the two parameters mentioned above and described below:
Installing Module:
Install the express module using the following command:
npm install express
Project structure:
Filename: Index.js
Run the index.js file using the following command:
node index.js
Output:
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.
Filename: Index.js
Calling multiple middleware from single middleware:
Filename: index.js
Output: Now open your browser, and 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 express.js module 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.
Project structure:
Filename: index.html
Filename: app.js
Steps to run the program:
Run the app.js file using the following command:
node app.js
Output: