[英]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.