如何让中间件响应每个请求
How to make middleware respond on every request
我正在 express
中测试 middleware
,我 运行 遇到了问题。
在我的第二行,我使用 app.use
调用 testOne 和 testTwo。当我在浏览器中访问我的根 / 时,这两个 middleware
功能 运行。但是,如果我访问 运行dom 静态文件,例如 image.png 或 about.htm,它们不会触发。无论我请求什么文件,我如何让它们启动?非常感谢您的帮助!
app.use(express.static(path.join(__dirname, 'public')));
app.use(testOne, testTwo);
function testOne(request, response, next) {
console.log('testOne ran');
}
function testTwo(request, response, next) {
console.log('testTwo ran');
}
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname, 'public/index.htm'));
});
所有中间件都必须调用 next()
才能继续路由到下一个路由处理程序。
app.use(testOne, testTwo);
function testOne(request, response, next) {
console.log('testOne ran');
next();
}
function testTwo(request, response, next) {
console.log('testTwo ran');
next();
}
app.use(express.static(path.join(__dirname, 'public')));
如果你不调用 next()
路由就会停止,什么都不做(直到它可能最终超时)。
此外,如果您希望这些中间件在所有请求上触发,那么您需要将它们放在第一位,然后放在可能实际处理请求的其他请求处理程序之前,例如 express.static()
。
我正在 express
中测试 middleware
,我 运行 遇到了问题。
在我的第二行,我使用 app.use
调用 testOne 和 testTwo。当我在浏览器中访问我的根 / 时,这两个 middleware
功能 运行。但是,如果我访问 运行dom 静态文件,例如 image.png 或 about.htm,它们不会触发。无论我请求什么文件,我如何让它们启动?非常感谢您的帮助!
app.use(express.static(path.join(__dirname, 'public')));
app.use(testOne, testTwo);
function testOne(request, response, next) {
console.log('testOne ran');
}
function testTwo(request, response, next) {
console.log('testTwo ran');
}
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname, 'public/index.htm'));
});
所有中间件都必须调用 next()
才能继续路由到下一个路由处理程序。
app.use(testOne, testTwo);
function testOne(request, response, next) {
console.log('testOne ran');
next();
}
function testTwo(request, response, next) {
console.log('testTwo ran');
next();
}
app.use(express.static(path.join(__dirname, 'public')));
如果你不调用 next()
路由就会停止,什么都不做(直到它可能最终超时)。
此外,如果您希望这些中间件在所有请求上触发,那么您需要将它们放在第一位,然后放在可能实际处理请求的其他请求处理程序之前,例如 express.static()
。