javascript - NodeJs routing middleware error -
i trying implement middleware check if user authenticated before server delivers page. although looks process of doing simple, node throwing error says "can't set headers after sent".
my router's code is:
module.exports = function(app) { app.get('/', checkauth, require('./myauthenticatedpage').get); app.get('/login', require('./myloginpage').get); };
the myauthenticatedpage.js:
exports.get = function(req, res) { res.render('index'); };
the myloginpage.js:
exports.get = function(req, res) { res.render('login'); };
the checkauth.js:
module.exports = function (req, res, next) { if(!req.session.user) { res.redirect('/login'); } next(); }
any on appreciated.
thanks
if aren't authenticated, you'll redirect user , try render index page. causes http headers sent twice, hence error "can't set headers after sent".
in checkauth.js try:
module.exports = function (req, res, next) { if(!req.session.user) { res.redirect('/login'); } else { next(); } }
Comments
Post a Comment