简体   繁体   中英

Express.js Rout Redirect Error: Can't set headers after they are sent

I am not sure why I am getting this error, "Error: Can't set headers after they are sent." It is simple API built on express.js which will check whether the user is logged in or not and if the user is not logged in, it would just take the user to the login page.

When you look at the code below starting from fetch(authApiUrl, config) , if the user is logged in, it would give status of 200. If the user is not logged in, it would give status for 401 and it would go into the "else" statement and initiate redirectToAccountSignin(request, response, wwwS); .

Then it would go into the redirectToAccountSignin function. So far, when I run this code, it does go into redirectToAccountSignin function, but I believe it throws error on response.redirect(wwwS + '/account/signin?method=initialize&TARGET=' + encodedTargetUrl); .

Is there a problem with my "redirect" method? What am I doing wrong? Can anyone please help me with this?

Thank you in advance.

const fetch = require("node-fetch");
const splunkLogFormat = require('./logUtilities');

function authenticate(request, response, next, successCallback, configuration) {
  // get environment for URL call and grab from environment json
  const appName = !!configuration.appName ? configuration.appName : 'micro-server-app';
  const runtimeEnvironment = !!configuration.environment ? configuration.environment : 'dev';

  const logPreamble = splunkLogFormat(appName, 'authenticate');
  const wwwS = !!environments && !!environments[runtimeEnvironment] && environments[runtimeEnvironment].www_s;

  const authApiUrl = wwwS + '/api/auth/login/check';
  const headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    cookie: request.headers.cookie
  };
  const method = 'GET';
  const config = { headers, method };
  fetch(authApiUrl, config)
    .then(authResponse => {
      const status = authResponse.status;
      if (status === 200) {
        successCallback(request, response, next);
      } else {
        redirectToAccountSignin(request, response, wwwS);
      }
    })
    .catch(error => {
      redirectToAccountSignin(request, response, wwwS);
    });
};

function redirectToAccountSignin(request, response, wwwS) {
  const hostname = !!request && request.hostname;
  const protocol = 'https://';
  const url = !!request && request.originalUrl;
  const encodedTargetUrl = encodeURIComponent(protocol + hostname + url);

  response.redirect(wwwS + '/account/signin?method=initialize&TARGET=' + encodedTargetUrl);
  response.end();
};

module.exports = authenticate;

Are you sure you want to use res.end() after res.redirect() ? https://stackoverflow.com/a/54874227/4208845 What writing are you doing before that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM