Use Middleware to route user to the correct page. Given values from POST method exist in database

I am trying to use Middleware to check if the user entered the correct log in credentials to be routed to essentially the same page.

Goal: Have a log in page that takes user email and password. Check DB if account exists. If yes, then display the username on the top right hand corner of the navigation bar.

My server.js code handling the checking of the values passed from POST.

     router.post('/logInDynamic', (request, response, next) => {
  var email = request.body.email;
  console.log(email);
  if (email === 'Alex') {
    next();
    {
      response.setHeader('Content-Type', 'application/json');
      response.render('logInDynamic', {
        showUserInfo: true,
        layout: 'login',
      });
    }
  }
});

I am also getting this exception here below.

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Answers:

Answer

It looks like you are calling next() before setting the header and rendering. So the next middleware function starts running before this one finishes.

Essentially, that error means that your api has sent a response back to the client with the headers already so you can't overwrite it.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.