Angularjs 登录身份验证重定向获取 -- 错误:发送标头后无法设置标头

Angularjs Login authentication redirection getting -- Error: Can't set headers after they are sent

本文关键字:设置 身份验证 登录 重定向 获取 错误 Angularjs      更新时间:2023-09-26

对NodeJS/AngularJS来说非常新。验证 id 和密码后,我无法将登录页面重定向到另一个页面。

控制器中的代码如下,由路由"/login/check"触发:

exports.logincheck = function(req, res) {
User.findOne({email: req.body.email}).exec(function (err,user) {
    if (!user) {
        err = 'User not found.';
    } else if (user.hashed_password == hashPW(req.body.password.toString())) {
        req.session.regenerate(function () {
            req.session.user = user.id;
            req.session.username = user.username;
            req.session.msg = 'Authenticated as ' + user.username ;
            res.redirect('/');
        });
    } else {
        err = 'Authentication failed.';
    }
if (err) {
    req.session.regenerate(function () {
        req.session.msg = err;
        res.status(404).send({err: 'User Not Found.'});
        res.redirect('/login');
    });
}
});

我的 AngularJS 代码如下:

var loginApp = angular.module('loginApp', []);
loginApp.controller('loginController', ['$scope', '$http', function($scope,$http){
    $scope.status = '';
    $scope.processForm = function(checkemail,checkpassword) {
        $http.post('/login/check', { email : checkemail, password: checkpassword})
            .success(function(data, status, headers, config) {
                $scope.user = data;
                $scope.error = "";
                $scope.status = status;
            })
            .error(function(data, status, headers, config) {
                $scope.user = {};
                $scope.error = data;    
                $scope.status = status;
            });
    };
}]);

只是尝试从我的 HTML 表单中传递 2 个变量进行验证。我得到的错误是:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.header (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:700:10)
at ServerResponse.send (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:154:12)
at fn (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:934:10)
at View.exports.renderFile [as engine] (/Users/josh_loh/Code/project/node_modules/ejs/lib/ejs.js:353:10)
at View.render (/Users/josh_loh/Code/project/node_modules/express/lib/view.js:93:8)
at EventEmitter.app.render (/Users/josh_loh/Code/project/node_modules/express/lib/application.js:566:10)
at ServerResponse.res.render (/Users/josh_loh/Code/project/node_modules/express/lib/response.js:938:7)
at /Users/josh_loh/Code/project/routes.js:52:7
at Layer.handle [as handle_request] (/Users/josh_loh/Code/project/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/josh_loh/Code/project/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/Users/josh_loh/Code/project/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/Users/josh_loh/Code/project/node_modules/express/lib/router/layer.js:82:5)
at /Users/josh_loh/Code/project/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/Users/josh_loh/Code/project/node_modules/express/lib/router/index.js:321:12)
at next (/Users/josh_loh/Code/project/node_modules/express/lib/router/index.js:261:10)

请帮忙。真的很想知道我理解错了什么。谢谢!

看起来控制器中至少有两个问题。

res.status(404).send({err: 'User Not Found.'});后跟res.redirect('/login');. sendredirect都完成请求并响应请求,所以这基本上就像尝试响应两次一样。 这可能就是给您带来"无法设置标题"错误的原因。 这个类似问题的答案很好地总结了何时应该使用各种 res 方法。

您还将在 req 对象上设置值。 Req 仅提供有关请求的信息,通常应被视为只读对象。 只有 res 对象中的数据才会返回到客户端。