是否可以为重定向添加响应标头? (快递)

时间:2021-04-22 02:28:47

标签: node.js express http

使用 NodeJS 中的 Express,我尝试重定向到另一个 url,然后在该重定向的响应中添加一个标头。这可能吗?例如,请求被重定向到 https://example.com,并且该响应的标头通常不会出现在来自 https://example.com 的响应中。

1 个答案:

答案 0 :(得分:0)

我认为您需要提及您希望重定向发生的位置!

如果它在一个单独的 API 请求中,例如,你可以创建一个中间件,你将它与重定向的请求一起使用

function Redirectmiddleware (req, res, next) => {

            res.header('HeaderName', value)

    next()
}

然后您可以将中间件与将处理重定向的 API 请求一起使用

app.post('/api/redirect', Redirectmiddleware , YourHandlerHere)
 // here you can use the same handler you are using for the https://example.com
//only this redirect request will have the header you passed