【发布时间】:2021-04-22 02:28:47
【问题描述】:
使用 NodeJS 中的 Express,我尝试重定向到另一个 url,然后在该重定向的响应中添加一个标头。这可能吗?例如,一个请求被重定向到https://example.com,并且该响应的标头通常不会出现在来自https://example.com 的响应中。
【问题讨论】:
使用 NodeJS 中的 Express,我尝试重定向到另一个 url,然后在该重定向的响应中添加一个标头。这可能吗?例如,一个请求被重定向到https://example.com,并且该响应的标头通常不会出现在来自https://example.com 的响应中。
【问题讨论】:
我认为您需要提及您希望重定向发生的位置!
如果它在一个单独的 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
【讨论】: