ユーザーの資格情報をチェックするミドルウェアを作成しようとしていますが、成功した場合はユーザー情報でJWTを作成します。私はクッキーを作成し、そのクッキー内にJWTを保存したいと思いますが、正しく動作させることができません。投稿時にログインメソッドを起動した後、「Can not POST/authenticate」というエラー404が表示されます。私は何が欠けていますか?NodeJS 404エラー
ルート:
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();
});
};
最後に何かを送る必要がありますか(この場合は「OK」)、res.end()を呼び出すことができます。 ? –
'res.end()'を呼び出すだけで、HTTPステータス200の空の本体が返されます –