Express Webapp上的Passport.js验证错误“一旦发送,就无法设置集头”

时间:2019-05-19 16:42:51

标签: node.js express passport.js

嗨,我正在制作一个连接到我的大学提供的远程数据库上的mysql服务器的express webapp。我正在按照一个教程来设置passport.js身份验证,并且在尝试发布有关/ regnew请求的数据时遇到问题

油灰显示的错误是:

错误:/students/danu7_tc3/project/views/error.hbs:发送标头后无法设置标头。     在validateHeader(_http_outgoing.js:491:11)     在ServerResponse.setHeader(_http_outgoing.js:498:3)     在ServerResponse.header(/students/danu7_tc3/project/node_modules/express/lib/response.js:767:10)     在ServerResponse.send(/students/danu7_tc3/project/node_modules/express/lib/response.js:170:12)     完成时(/students/danu7_tc3/project/node_modules/express/lib/response.js:1004:10)     在/students/danu7_tc3/project/node_modules/hbs/lib/hbs.js:93:9     在Object.done(/students/danu7_tc3/project/node_modules/hbs/lib/async.js:74:20)     在/students/danu7_tc3/project/node_modules/hbs/lib/hbs.js:88:18     在/students/danu7_tc3/project/node_modules/hbs/lib/hbs.js:69:11     在Object.done(/students/danu7_tc3/project/node_modules/hbs/lib/async.js:74:20)

这是我的index.js代码

var express = require('express');
var router = express.Router();

var passport = require('passport');

var bcrypt = require('bcrypt');
const saltRounds = 10;


/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', {
        title: 'Express'
    });
});

router.get('/home', function(req, res, next) {
    res.render('home', {
        title: 'Home'
    });
});


router.get('/registration', function(req, res, next) {
    res.render('registration');

});
router.post('/regnew', function(req, res, next) {

    var username = req.body.username;
    var email = req.body.email;
    var password = req.body.password;


    console.log(username);


    const db = require('./db_connection.js');

    bcrypt.hash(password, saltRounds, function(err, hash) {
        db.query('INSERT INTO Users (username, email, password) VALUES 
            ( ? , ? , ? )
            ',[username,email,hash], function(error,result,fields) {
                if (error) throw error;

                db.query('SELECT LAST_INSERT_ID() as user_id', function(error,
                    results, fields) {
                    if (error) throw error;

                    const user_id = results[0];


                    console.log(results[0]);
                    req.login(user_id, function(err) {
                        res.redirect('/home');
                    });



                });
                res.render('registration', {
                    title: 'Success'
                });
            });
    });

});
passport.serializeUser(function(user_id, done) { //store user id in 
    session
    done(null, user_id);
});

passport.deserializeUser(function(id, done) { //read from the session

    done(err, user_id);
});



module.exports = router;

1 个答案:

答案 0 :(得分:0)

您两次致电res

  1. res.redirect
  2. res.render