我的 Express/Node.js 应用程序中有一个中间件来保护特定路由。
问题是,当我使用这个中间件时,如果凭据检查错误,我会收到请求超时
这是我当前使用的代码:
var express = require('express');
var bodyParser = require('body-parser');
// Set Header because cross-domain call
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://docker.local");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
next();
});
function checkAuth(req, res, next){
if(// Credentials right){
next();
} else {
// If code enter in that case, it will generate a Timeout
// even with next(); ...
}
}
// Use checkAuth to protect route
app.get('/server', checkAuth, getMessages);
app.get('/', function(req, res){
res.status(200).send('Slack Server is listening for outgoing webhooks ...');
});
问题是:为什么我会超时?
编辑
如果我使用浏览器发出 GET 请求,则会出现请求超时。但是,如果我以其他方式触发 GET 请求,则不会出现超时并且代码可以正常工作。 GET/favicon
是否有可能以某种方式搞乱了这些东西?
最佳答案
app.use
要求您调用 next
继续处理堆栈中的下一个中间件或请求。
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://docker.local");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
next();
});
关于javascript - Express中间件意味着请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34875445/