为什么添加 get 参数会强制执行中间件
Why adding a get parameter forces middleware execution
我有这样的中间件
// route middleware to verify a token
app.use(function(req, res, next) {
console.log(req.baseUrl);
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
return res.json({
success: false,
message: 'Failed to authenticate token.'
});
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
});
此路由 http://localhost:8080/verifyAccount 没有响应 No token provided
app.get('/verifyAccount', function (req, res) {
res.json({ message: 'Welcome to verify account!' });
});
但是下面的路线 http://localhost:8080/verifyAccount?id=123 可以:
app.get('/verifyAccount/:id', function (req, res) {
res.json({ message: 'Welcome to verify account!' });
});
中间件代码在代码文件底部,获取路径在上
背后的概念是什么?
Why adding a get parameter forces middleware execution?
刚刚发现如果我这样称呼它http://localhost:8080/verifyAccount/id=123,它正确returns Welcome to verify account!
发现问题出在调用路由的方式上。感谢 Thomas Theiner 的帮助。
带有查询参数 ?id=123
的请求与 /:id
不匹配。它应该被称为 verifyAccount/123
而不是。
Since, the route ?id=123
did not matched any of the path. Hence, was finally reaching the middleware
for execution
位置决定参数,而不是名称。该名称仅在节点代码中用于引用参数。
对于多个参数,我们将有多个斜杠,如 verifyAccount/:id/:otherParameter
,将使用 verifyAccount/123/234
调用
我有这样的中间件
// route middleware to verify a token
app.use(function(req, res, next) {
console.log(req.baseUrl);
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
return res.json({
success: false,
message: 'Failed to authenticate token.'
});
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
});
此路由 http://localhost:8080/verifyAccount 没有响应 No token provided
app.get('/verifyAccount', function (req, res) {
res.json({ message: 'Welcome to verify account!' });
});
但是下面的路线 http://localhost:8080/verifyAccount?id=123 可以:
app.get('/verifyAccount/:id', function (req, res) {
res.json({ message: 'Welcome to verify account!' });
});
中间件代码在代码文件底部,获取路径在上
背后的概念是什么?
Why adding a get parameter forces middleware execution?
刚刚发现如果我这样称呼它http://localhost:8080/verifyAccount/id=123,它正确returns Welcome to verify account!
发现问题出在调用路由的方式上。感谢 Thomas Theiner 的帮助。
带有查询参数 ?id=123
的请求与 /:id
不匹配。它应该被称为 verifyAccount/123
而不是。
Since, the route
?id=123
did not matched any of the path. Hence, was finally reaching themiddleware
for execution
位置决定参数,而不是名称。该名称仅在节点代码中用于引用参数。
对于多个参数,我们将有多个斜杠,如 verifyAccount/:id/:otherParameter
,将使用 verifyAccount/123/234