简体   繁体   English

NodeJS 404错误

[英]NodeJS 404 Error

I am trying to create a middleware that will check user credentials, and if successful, create a JWT with the user information. 我正在尝试创建一个将检查用户凭据的中间件,如果成功,则使用用户信息创建一个JWT。 I want to create a cookie and then store the JWT within the cookie, but I can't seem to get it working properly. 我想创建一个cookie,然后将JWT存储在cookie中,但是我似乎无法使其正常工作。 After hitting the login method on post, I get a 404 'Not Found' error, saying it "Cannot POST /authenticate". 在发布时点击登录方法后,我收到404“未找到”错误,说它“无法发布/ authenticate”。 What am I missing? 我想念什么?

Route: 路线:

app.post('/authenticate', function(req, res, next){
    middleware.login(req, res, next);
});

Middleware: 中间件:

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();
    });
};

The reason you are receiving a 404 not found is because you are actually not sending any response, but simply passing the execution to the next handler. 收到404 not found的原因是,您实际上没有发送任何响应,只是将执行传递给下一个处理程序。 As no handler is matched after that express returns 404. The app.post is actually called, but as you are calling next() it expects another middleware to handle the request. 由于在该Express返回404之后没有处理程序被匹配app.post实际上被调用,但是当您调用next()它期望另一个中间件来处理请求。 Here is a pretty basic example that your route is actually invoked, but the execution is passed to the next middleware in the Chain of responsibility. 这是一个非常基本的示例,您的路由实际上已被调用,但是执行被传递给责任链中的下一个中间件。 As there is none - you receive the error. 由于没有-您会收到错误消息。

 var express = require('express'); var app = express(); app.post('/test', (req, res, next) => { console.log('called'); next(); }); app.listen(5000); 

This will write 'called' to the console, but still return 404. What you can do is add another handler after successful authentication which would look something like this: 这会将“ called”写入控制台,但仍返回404。您可以做的是在成功通过身份验证后添加另一个处理程序,如下所示:

 app.post('/authenticate', (req, res, next) => { res.send('OK').end(); }); 

or you can incorporate that login in the middleware itself instead of calling next() 或者您可以将该登录名合并到中间件本身中,而不用调用next()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM