Express is the next function, what is it really for?

There have been attempts to find a good description of what the next() method does. The Express documentation says that next('route') can be used to go to this route and skip all routes between them, but sometimes next is called without arguments. Does anyone know a good tutorial etc. which describes the function next ?

+68
express
Oct 30 '12 at 5:18
source share
4 answers

next() without arguments says: "Just kidding, I don’t want to deal with this." He returns and tries to find the next route that will match.

This is useful, say, if you want to have some kind of page manager with URLs, as well as many other things, but here is an example.

 app.get('/:pageslug', function(req, res, next){ var page = db.findPage(req.params.pageslug); if (page) { res.send(page.body); } else { next(); } }); app.get('/other_routes', function() { //... }); 

This code should check the database for the page with a specific identifier. If he finds, do it! if it does not find it, then ignore this route handler and check for others.

So next() without arguments allows you to pretend that you did not process the route so that you can pick it up instead.




Or a counter labeled app.all('*') . This allows you to execute some kind of general installation code, and then move on to other routes to do something more specific.

 app.all('*', function(req, res, next){ myHitCounter.count += 1; next(); }); app.get('/other_routes', function() { //... }); 
+99
Oct 30 '12 at 5:26
source share

In most frameworks, you receive a request and want to return a response. Due to the asynchronous nature of Node.js, you are having problems with nested callbacks if you are doing non-trivial things. To prevent this from happening, Connect.js (before version 4.0, Express.js was a layer on top of connect.js) has something called middleware, which is a function with 2, 3 or 4 parameters.

 function (<err>, req, res, next) {} 

Your Express.js application is a stack of these features.

enter image description here

A router is special; it is middleware that allows one or more middleware to be run for a specific URL. So this is the stack inside the stack.

So what to do next? Simply, it tells your application to run the following middleware. But what happens when you pass on something further? Express will interrupt the current stack and start all middleware with 4 parameters.

 function (err, req, res, next) {} 

This middleware is used to handle any errors. I like to do the following:

 next({ type: 'database', error: 'datacenter blew up' }); 

With this error, I will probably tell the user that something went wrong and will register a real error.

 function (err, req, res, next) { if (err.type === 'database') { res.send('Something went wrong user'); console.log(err.error); } }; 

If you present the Express.js application as a stack, you can probably fix a lot of weirdness. For example, when you add your cookie middleware after the router, it makes sense that your routes will not have cookies.

+95
Oct 30 '12 at 18:41
source share

IMHO, the accepted answer to this question is not very accurate. Others answered correctly, but I wanted to provide a little more code to make it more specific. Let's say you have this simple express application:

 var express = require('express'); var app = express(); app.get('/user/:id', function (req, res, next) { console.log('before request handler'); next(); }); app.get('/user/:id', function (req, res, next) { console.log('handling request'); res.sendStatus(200); next(); }); app.get('/user/:id', function (req, res, next) { console.log('after request handler'); next(); }); app.listen(3000, function () { console.log('Example app listening on port 3000!') }); 

If you do

 curl http://localhost:3000/user/123 

you will see that this is printed on the console:

 before request handler handling request after request handler 

Now, if you comment on the next() call in the middle handler as follows:

 app.get('/user/:id', function (req, res, next) { console.log('handling request'); res.sendStatus(200); //next(); }); 

You will see this on the console:

 before request handler handling request 

Note that the last handler (the one that prints after request handler ) does not start. This is because you no longer say express to start the next handler.

That way, it doesn't really matter if your β€œmain” handler (the one that returns 200) was successful or not, if you want the rest of the other participants to run, you should call next() .

For example, let's say you want to register all the requests coming to any database, regardless of whether the request succeeded. If you process this in the middleware after the main request handler, you must call next() in the main handler, or the log will not start.

+7
08 Sep '17 at 18:12
source share

next () without a parameter calls the next route handler within the framework.

+4
Jun 24 '15 at 20:23
source share



All Articles