【问题标题】:Is it possible to add a response header for a redirect? (Express)是否可以为重定向添加响应标头? (表达)
【发布时间】:2021-04-22 02:28:47
【问题描述】:

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

【问题讨论】:

标签: node.js express http


【解决方案1】:

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

如果它在一个单独的 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

【讨论】:

  • 谢谢,虽然这对于不是到 api 的重定向是可能的吗?
  • 是的,您可以重定向到某个 URL,方法是执行类似 res.redirect('http: //example .com') 或路径 res.redirect('/user')
猜你喜欢
  • 2011-02-24
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
相关资源
最近更新 更多