我正在通过我的Express配置进行API代理,如下所示。
我正在通过我的Express配置进行API代理,如下所示。
// Proxy api calls
app.use('/api', function (req, res) {
let url = config.API_HOST + req.url
req.pipe(request(url)).pipe(res)
})
config.API_HOST
在这里解析为我的API网址,req.url
是一些端点,例如 /users
。我尝试遵循npm文档中关于request的说明,并像下面那样设置了我的标头。
// Proxy api calls
app.use('/api', function (req, res) {
let options = {
url: config.API_HOST + req.url,
options: { 'mycustomheader': 'test' }
}
req.pipe(request(options)).pipe(res)
})
但是我无法在Chrome开发者工具的网络选项卡中看到我的定制请求头。
req.get("abc")
读取,并且对于通过req.headers.abc = 'xyz'
添加的标头将返回undefined
。另一方面,const x = req.headers.abc
将返回通过req.headers.abc = 'xyz'
创建的标头的值,而对于实际接收到的请求中包含的标头将返回undefined
。 - Mon