This article mainly introduces nodejs’s understanding of the next function in express, which has certain reference value. Interested friends can refer to it
Recently, the company is using node to separate the front and back ends. The web framework used is express, so I have an in-depth understanding of the express framework. I wrote an article about express routing some time ago, but it seems that a very important content is missing in that article, which is the next of express, so today Let’s talk about express’s next separately.
Regarding next, we will mainly explain it from three points:
What is the role of next?
When should we use next?
#What is the internal implementation mechanism of next?
The role of Next
When we define the express middleware function, we will define the third parameter as next, and this next is our Today’s protagonist, the next function is mainly responsible for handing control to the next middleware. If the current middleware does not terminate the request and next is not called, then the request will be suspended and the middleware defined later will not be executed. Opportunity.
When to use Next
We already know from the above description that the next function is mainly used to ensure that all registered middleware are executed one by one, then We should call the next function in all middleware, but there is a special case. If the middleware we define terminates this request, then the next function should not be called again, otherwise problems may occur. Let's look at this section. Code
app.get('/a', function(req, res, next) { res.send('sucess'); next(); }); // catch 404 and forward to error handler app.use(function(req, res, next) { console.log(404); var err = new Error('Not Found'); err.status = 404; next(err); }); app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); });
sends request "/a", the console prints the log as follows:
404 GET /a 500 6.837 ms - - Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:345:11)
Why does the code throw The exception is because we called the next function after res.send. Although our request has been terminated, the subsequent 404 middleware will still be executed, and the subsequent middleware attempts to add attributes to the headers of res. value, so the above exception will be thrown.
You may have a question after reading this. If I don't call the next function after res.send, will the 404 middleware defined later never be executed? Now we delete the next function call after res.send and send the request "/xxx", we will find that the 404 middleware is executed, (ㄒoㄒ), isn't this contradictory to what we said before, our customization The middleware does not call next, but the middleware defined later is still executed. Why is this? It seems that we can only ask for help from the source code~~~
Next’s internal mechanism
##
function next(err) { ... //此处源码省略 // find next matching layer var layer; var match; var route; while (match !== true && idx < stack.length) { layer = stack[idx++]; match = matchLayer(layer, path); route = layer.route; if (typeof match !== 'boolean') { // hold on to layerError layerError = layerError || match; } if (match !== true) { continue; } ... //此处源码省略 } ... //此处源码省略 // this should be done for the layer if (err) { layer.handle_error(err, req, res, next); } else { layer.handle_request(req, res, next); } }
function next(err) { if (err && err === 'route') { return done(); } var layer = stack[idx++]; if (!layer) { return done(err); } if (layer.method && layer.method !== method) { return next(err); } if (err) { layer.handle_error(err, req, res, next); } else { layer.handle_request(req, res, next); } }
Layer.prototype.handle_error = function handle_error(error, req, res, next) { var fn = this.handle; if (fn.length !== 4) { // not a standard error handler return next(error); } try { fn(error, req, res, next); } catch (err) { next(err); } };
The above is the detailed content of Detailed explanation of the usage of next function in express in nodejs. For more information, please follow other related articles on the PHP Chinese website!