Can't set headers after they are sent. Express JS

10,706

Solution 1

It seems that in your functions, the order of function arguments is wrong.

Instead of (err, res) make (req, res) or (err, req, res, next). Hope this helps.

Solution 2

Replace next() with return false.

Share:
10,706
Subiyantoro
Author by

Subiyantoro

Updated on June 04, 2022

Comments

  • Subiyantoro
    Subiyantoro almost 2 years

    Help me :(, i cannot find solution for my error, i try using return before my response it dosen't work

    this my code

    function isAuthenticated(req, res, next) {
    var getToken = req.cookies.token;
    var getUrl = req.originalUrl;
    console.log(getToken)    
    if(getToken == undefined) {
        res.redirect('/login')
        next()
    } else {
        console.log('success')
        next()
    }
    next() }
    
    router.get('/login', isAuthenticated, auth.login);
    router.get('/logout', isAuthenticated, auth.logout);
    router.post('/logging', auth.prosesLogin);