为"请求"添加自定义标头

Ilj*_*lja 8 api header request node.js express

我通过以下设置在我的快速配置中代理我的api

  // Proxy api calls
  app.use('/api', function (req, res) {
    let url = config.API_HOST + req.url
    req.pipe(request(url)).pipe(res)
  })
Run Code Online (Sandbox Code Playgroud)

config.API_HOST在这里解析为我的api url并且req.url是一些端点,即/users我尝试在npm上关注请求的文档并设置我的标题如此

  // 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)
  })
Run Code Online (Sandbox Code Playgroud)

但我无法在网络下的chrome dev工具中看到我的自定义标题.

Ilj*_*lja 13

能够以这种方式实现它

  app.use('/api', function (req, res) {
    let url = config.API_HOST + req.ur
    req.headers['someHeader'] = 'someValue'
    req.pipe(request(url)).pipe(res)
  })
Run Code Online (Sandbox Code Playgroud)