Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将自定义标题添加到';请求';_Node.js_Api_Express_Header_Request - Fatal编程技术网

Node.js 将自定义标题添加到';请求';

Node.js 将自定义标题添加到';请求';,node.js,api,express,header,request,Node.js,Api,Express,Header,Request,我正在通过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\u HOST解析为我的API url,而req.url是某个端点,即/users我尝试在npm上遵循文档,并像这样设置我的头

我正在通过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\u 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)
  })

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

就是通过这种方式实现的

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

由于一些奇怪的原因,
req.setHeader('someHeader','somValue')
对我不起作用


但是
req.headers['someHeader']='someValue'
这是有效的

所以当你向/api发出get请求时,它将被发送一个带有'someValue'值的'someHeader'头?这对我来说也是有效的。我试着用res.setHeader设置标题,但是没有用。req['token]=“某些令牌”工作正常。我想知道它是否安全..
req
没有“setHeader”功能。“setHeader”功能仅在
res
对象上存在。