node.js 路由后的 Node Express 4 中间件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24258782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node Express 4 middleware after routes
提问by goofballLogic
Following the upgrade to Express 4, and the removal of app.router, I'm struggling to get middleware to execute after routes execute.
在升级到 Express 4 并移除 app.router 之后,我正在努力让中间件在路由执行后执行。
e.g. the following code correctly responds with "hello", but never calls the configured middleware
例如,以下代码正确响应“hello”,但从不调用配置的中间件
var express = require( "express" )();
express.get( "/", function( req, res ) {
res.send( "hello" );
} );
express.use( function( req, res, next ) {
console.log( "world" );
next();
} );
express.listen( 8888 );
CLARIFICATION:
澄清:
the following code shows "before" on the console, but not "after":
以下代码在控制台上显示“之前”,但不显示“之后”:
var express = require( "express" )();
express.use( function( req, res, next ) {
console.log( "before" );
next();
} );
express.get( "/", function( req, res ) {
res.send( "hello" );
} );
express.use( function( req, res, next ) {
console.log( "after" );
next();
} );
express.listen( 8888 );
采纳答案by Joseph Snow
In regards to Express 4, the "after" function from your second example never gets called because the middle function never calls next().
关于 Express 4,第二个示例中的“after”函数永远不会被调用,因为中间函数永远不会调用 next()。
If you want the "after" function to get called, then you need to add and call the next callback from your middle function like this:
如果您希望调用“after”函数,那么您需要像这样从中间函数添加和调用下一个回调:
var express = require( "express" )();
express.use( function( req, res, next ) {
console.log( "before" );
next();
} );
express.get( "/", function( req, res, next ) {
res.send( "hello" );
next(); // <=== call next for following middleware
} );
express.use( function( req, res, next ) {
console.log( "after" );
next();
} );
express.listen( 8888 );
res.send()writes the headers and response back to the client.
res.send()将标头和响应写回客户端。
Beware, that once res.send() has been called, you won't want to update your response headers or contents. But you can do other tasks like database updates or logging.
请注意,一旦 res.send() 被调用,您就不会想要更新您的响应头或内容。但是您可以执行其他任务,例如数据库更新或日志记录。
Note that express looks at the the number of arguments in the middleware function and does different logic. Take express error handlersfor example, which have 4 parameters defined.
请注意,express 查看中间件函数中的参数数量并执行不同的逻辑。以express 错误处理程序为例,它定义了 4 个参数。
express error handler signature: app.use(function(err, req, res, next) {});
表示错误处理程序签名: app.use(function(err, req, res, next) {});
Calling next on the very last item in your middleware chain is optional, but probably a good idea in case you ever change things around.
在中间件链中的最后一项上调用 next 是可选的,但如果您改变了一些事情,这可能是一个好主意。
回答by Test
The correct answer is using the res.on("finish", cb)callback.
正确答案是使用res.on("finish", cb)回调。
i.e.:
IE:
express.use(function(req, res, next) {
console.log("before");
res.on("finish", function() {
console.log("after");
});
next();
});
回答by marcosnils
Have you checked putting your console.log after the next() call?
您是否检查过在 next() 调用之后放置您的 console.log ?
express.use( function( req, res, next ) {
next();
console.log( "world" );
});
express.get( "/", function( req, res ) {
res.send( "hello" );
});
回答by vinayr
Order is important http://expressjs.com/4x/api.html#app.use
顺序很重要http://expressjs.com/4x/api.html#app.use
express.use( function( req, res, next ) {
console.log( "world" );
next();
});
express.get( "/", function( req, res ) {
res.send( "hello" );
});
回答by Loganathan .R
you can use the Middle ware function in different js file and you can use require function. so that it will be called before and after the http request.
您可以在不同的 js 文件中使用中间件功能,也可以使用 require 功能。以便在 http 请求之前和之后调用它。
index.js:
const logger = require("./logger");
const express = require("express");
var app = express();
app.listen("3000",()=>console.log("listening on 3000..."))
app.use(logger("AppServer"));
//get expression
app.get("/", function(req,res){
console.log("res not received");
res.send("Hello World");
console.log("res received");
})
logger.js
module.exports = (applicationName) =>{
return function log(req,res,next){
console.log(applicationName+" started "+os.hostname);
res.on("finish",()=>{
console.log(applicationName+" completed "+os.hostname);
})
next();
}};
output:
AppServer started hostname-PC
res not received
res received
AppServer completed hostname-PC
Note: in the logger.js instead of using res.on("finish",callback), you can use req.on("end",callback)
注意:在 logger.js 中可以使用 req.on("end",callback) 而不是使用 res.on("finish",callback)

