title: res.redirect Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
body: <p>Keep getting this error pointing back to a res.redirect... I thought adding a 'return' would fix it since it allows the middleware to end but error persisting</p>
  <p>heres the function</p>
  <pre><code>app.post('/login', (req,res) =&gt; {
     //set this up with passport ideally... auth with db 
     var username = req.body.email
     var password = req.body.password
    
     User.findOne({email:username})
     .then(user =&gt; {
        if(user) {
           bcrypt.compare(password, user.password, function(err, result) {
              if(err) {
                 //maybe send this back in res json?
                 console.log(err)
                 return res.redirect('/login')
              }
              else if(result){
                 //let token = jwt.sign({name:user.name}, )
                 req.session.user = user.dataValues
                 console.log('Login successful')
                 return res.redirect('/dashboard');       //error happening here
              }else {
                 console.log('Password does not match')
                 return res.redirect('/login')
              }
           })
        }
     })
     res.redirect('/')
  })
  </code></pre>
  <p>And here is my output</p>
  <pre><code>Login successful
  undefined:0
  
  
  
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:533:11)
      at ServerResponse.header (/Users/sherm/Documents/daily_email/node_modules/express/lib/response.js:771:10)
      at ServerResponse.location (/Users/sherm/Documents/daily_email/node_modules/express/lib/response.js:888:15)
      at ServerResponse.redirect (/Users/sherm/Documents/daily_email/node_modules/express/lib/response.js:926:18)
      at /Users/sherm/Documents/daily_email/server.js:79:27 {
    code: 'ERR_HTTP_HEADERS_SENT'
  }
  npm ERR! code ELIFECYCLE
  </code></pre>