简体   繁体   English

Express.js 路由重定向错误:发送后无法设置标头

[英]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.它是建立在 express.js 上的简单 API,它将检查用户是否登录,如果用户未登录,它只会将用户带到登录页面。

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);当您查看以下从fetch(authApiUrl, config)开始的代码时,如果用户已登录,则状态为 200。如果用户未登录,则状态为 401,然后进入“else”语句并启动redirectToAccountSignin(request, response, wwwS); . .

Then it would go into the redirectToAccountSignin function.然后它会进入redirectToAccountSignin函数。 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);到目前为止,当我运行此代码时,它确实进入了redirectToAccountSignin函数,但我相信它会在response.redirect(wwwS + '/account/signin?method=initialize&TARGET=' + encodedTargetUrl);上引发错误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() ?您确定要在res.end()之后使用res.redirect()吗? https://stackoverflow.com/a/54874227/4208845 What writing are you doing before that? https://stackoverflow.com/a/54874227/4208845在那之前你在写什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js和Express.js错误:发送标头后无法设置标头 - Node.js and Express.js Error : Can't set headers after they are sent 节点express.js在发送标头后无法设置标头。” - node express.js Can't set headers after they are sent.' 错误:发送标头后无法设置标头。 表达js - Error: Can't set headers after they are sent. express js Node.js Express.js Mongoose 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Node.js Express.js Mongoose Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 使用 express.js 我收到此错误:未捕获的错误 [ERR_HTTP_HEADERS_SENT]: 发送到客户端后无法设置标头 - Using express.js I get this error: Uncaught Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client node.js / express:发送标头后无法设置标头 - node.js/express: Can't set headers after they are sent Express / node.js发送后无法设置标头 - Express / node.js Can't set headers after they are sent 在Express JS中发送标头后无法设置标头 - Can't set headers after they are sent in express js Node Express MySQL和错误:发送标头后无法设置标头 - Node Express MySQL and the Error: Can't set headers after they are sent Express 4错误:发送标头后无法设置标头 - Express 4 Error: Can't set headers after they are sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM