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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -