节点 404 错误
NodeJS 404 Error
我正在尝试创建一个中间件来检查用户凭据,如果成功,则创建一个包含用户信息的 JWT。我想创建一个 cookie,然后将 JWT 存储在 cookie 中,但我似乎无法让它正常工作。在 post 上点击登录方法后,我收到 404 'Not Found' 错误,说是 "Cannot POST /authenticate"。我错过了什么?
路线:
app.post('/authenticate', function(req, res, next){
middleware.login(req, res, next);
});
中间件:
exports.login = function(req, res, next){
var username = req.body.username;
var password = req.body.password;
User.findByUsername(username,function(err, user){
if(err){
res.send({ success: false, message: 'Authentication failed.' });
}
if (!user) {
res.send({ success: false, message: 'Authentication failed. User not found.' });
}
if(user && !user.isuserenabled){
res.send({ success: false, message: 'Authentication failed. User not found.' });
}
if (!UserSchema.comparePassword(password,user.user_password )) {
res.send({ success: false, message: 'Authentication failed. User not found.' });
}
res.cookie('yummyCookie', jwt.sign(
//payload, secret, options, [callback]
{
id: user.user_id,
email: user.email,
name: user.firstname + " " + user.lastname,
role: user.role
},
config.secret, // DO NOT KEEP YOUR SECRET IN THE CODE
{expiresIn: "1h"}, {secure: true, httpOnly: true}));
return next();
});
};
您收到 404 not found
的原因是您实际上没有发送任何响应,而只是将执行传递给下一个处理程序。由于在表达 returns 404 之后没有匹配的处理程序。
app.post
实际上被调用了,但是当您调用 next()
时,它需要另一个中间件来处理请求。这是一个非常基本的示例,您的路由实际被调用,但执行被传递给责任链中的下一个中间件。由于存在 none - 您会收到错误消息。
var express = require('express');
var app = express();
app.post('/test', (req, res, next) => {
console.log('called');
next();
});
app.listen(5000);
这会将 'called' 写入控制台,但仍然是 return 404。您可以做的是在成功验证后添加另一个处理程序,如下所示:
app.post('/authenticate', (req, res, next) => {
res.send('OK').end();
});
或者您可以将该登录合并到中间件本身而不是调用 next()
我正在尝试创建一个中间件来检查用户凭据,如果成功,则创建一个包含用户信息的 JWT。我想创建一个 cookie,然后将 JWT 存储在 cookie 中,但我似乎无法让它正常工作。在 post 上点击登录方法后,我收到 404 'Not Found' 错误,说是 "Cannot POST /authenticate"。我错过了什么?
路线:
app.post('/authenticate', function(req, res, next){
middleware.login(req, res, next);
});
中间件:
exports.login = function(req, res, next){
var username = req.body.username;
var password = req.body.password;
User.findByUsername(username,function(err, user){
if(err){
res.send({ success: false, message: 'Authentication failed.' });
}
if (!user) {
res.send({ success: false, message: 'Authentication failed. User not found.' });
}
if(user && !user.isuserenabled){
res.send({ success: false, message: 'Authentication failed. User not found.' });
}
if (!UserSchema.comparePassword(password,user.user_password )) {
res.send({ success: false, message: 'Authentication failed. User not found.' });
}
res.cookie('yummyCookie', jwt.sign(
//payload, secret, options, [callback]
{
id: user.user_id,
email: user.email,
name: user.firstname + " " + user.lastname,
role: user.role
},
config.secret, // DO NOT KEEP YOUR SECRET IN THE CODE
{expiresIn: "1h"}, {secure: true, httpOnly: true}));
return next();
});
};
您收到 404 not found
的原因是您实际上没有发送任何响应,而只是将执行传递给下一个处理程序。由于在表达 returns 404 之后没有匹配的处理程序。
app.post
实际上被调用了,但是当您调用 next()
时,它需要另一个中间件来处理请求。这是一个非常基本的示例,您的路由实际被调用,但执行被传递给责任链中的下一个中间件。由于存在 none - 您会收到错误消息。
var express = require('express');
var app = express();
app.post('/test', (req, res, next) => {
console.log('called');
next();
});
app.listen(5000);
这会将 'called' 写入控制台,但仍然是 return 404。您可以做的是在成功验证后添加另一个处理程序,如下所示:
app.post('/authenticate', (req, res, next) => {
res.send('OK').end();
});
或者您可以将该登录合并到中间件本身而不是调用 next()