You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+61
Original file line number
Diff line number
Diff line change
@@ -3777,6 +3777,67 @@ if (response.error) {
3777
3777
<b><a href="#">↥ back to top</a></b>
3778
3778
</div>
3779
3779
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:
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
+
<divalign="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
+
constexpress=require("express");
3817
+
constapp=express();
3818
+
3819
+
constmyLogger=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
+
**⚝[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
+
<divalign="right">
3838
+
<b><a href="#">↥ back to top</a></b>
3839
+
</div>
3840
+
3780
3841
#### Q. ***Is it possible to use "Class" in Node.js?***
3781
3842
#### Q. ***Explain Error Handling approaches in Node.js?***
3782
3843
#### Q. ***How would you handle errors for async code in Node.js?***
0 commit comments