我不确定为什么会收到此错误,“错误:发送标头后无法设置标头”。这是一个基于express.js的简单API,它将检查用户是否已登录,如果用户未登录,则只会将用户带到登录页面。
当您查看以下从fetch(authApiUrl, config)
开始的代码时,如果用户已登录,则其状态为200。如果用户未登录,则其状态为401,它将进入进入“ else”语句并启动redirectToAccountSignin(request, response, wwwS);
。
然后它将进入redirectToAccountSignin
函数。到目前为止,当我运行这段代码时,它确实进入了redirectToAccountSignin
函数,但是我相信它会在response.redirect(wwwS + '/account/signin?method=initialize&TARGET=' + encodedTargetUrl);
上引发错误。
我的“重定向”方法是否存在问题?我究竟做错了什么?有人可以帮我吗?
谢谢。
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;
答案 0 :(得分:-1)
确定要在--obfuscate
--save-obfuscation-map=<map-filename>
之后使用res.end()
吗? https://stackoverflow.com/a/54874227/4208845在那之前你在做什么?