Skip to content

Commit 381f3dd

Browse files
committed
Middleware functions
1 parent 4dfc92d commit 381f3dd

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,67 @@ if (response.error) {
37773777
<b><a href="#">↥ back to top</a></b>
37783778
</div>
37793779

3780+
## Q. ***What are the middleware functions in Node.js?***
3781+
3782+
Middleware functions are functions that have access to the **request object (req)**, the **response object (res)**, and the `next` function in the application\'s request-response cycle.
3783+
3784+
The `next` function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
3785+
3786+
Middleware functions can perform the following tasks:
3787+
3788+
* Execute any code.
3789+
* Make changes to the request and the response objects.
3790+
* End the request-response cycle.
3791+
* Call the next middleware in the stack.
3792+
3793+
If the current middleware function does not end the request-response cycle, it must call `next()` to pass control to the next middleware function. Otherwise, the request will be left hanging.
3794+
3795+
The following figure shows the elements of a middleware function call:
3796+
3797+
<p align="center">
3798+
<img src="assets/express-mw.png" alt="Middleware functions" width="800px" />
3799+
</p>
3800+
3801+
Middleware functions that return a Promise will call `next(value)` when they reject or throw an error. `next` will be called with either the rejected value or the thrown Error.
3802+
3803+
<div align="right">
3804+
<b><a href="#">↥ back to top</a></b>
3805+
</div>
3806+
3807+
## Q. ***Explain the use of next in node.js with example?***
3808+
3809+
The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
3810+
3811+
**Example:** Middleware function myLogger
3812+
3813+
To load the middleware function, call `app.use()`, specifying the middleware function. For example, the following code loads the **myLogger** middleware function before the route to the root path (/).
3814+
3815+
```js
3816+
const express = require("express");
3817+
const app = express();
3818+
3819+
const myLogger = function (req, res, next) {
3820+
console.log("LOGGED");
3821+
next();
3822+
};
3823+
3824+
app.use(myLogger);
3825+
3826+
app.get("/", (req, res) => {
3827+
res.send("Hello World!");
3828+
});
3829+
3830+
app.listen(3000);
3831+
```
3832+
3833+
**&#9885; [Try this example on CodeSandbox](https://codesandbox.io/s/next-function-nq042s)**
3834+
3835+
*Note: The `next()` function is not a part of the Node.js or Express API, but is the third argument that is passed to the middleware function. The `next()` function could be named anything, but by convention it is always named “next”. To avoid confusion, always use this convention.*
3836+
3837+
<div align="right">
3838+
<b><a href="#">↥ back to top</a></b>
3839+
</div>
3840+
37803841
#### Q. ***Is it possible to use "Class" in Node.js?***
37813842
#### Q. ***Explain Error Handling approaches in Node.js?***
37823843
#### Q. ***How would you handle errors for async code in Node.js?***

assets/express-mw.png

154 KB
Loading

0 commit comments

Comments
 (0)