如何在 MEAN 堆栈中设置 HTTP Header Key/Value 对

How to set a HTTP Header Key/Value Pair within the MEAN Stack

我正在使用 MEAN 堆栈,需要在我的网络应用程序中设置 HTTP header key/value 对。

my_key: my_value

使用Angular5,发现如下代码:

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

但不确定如何应用到我的要求,因为我认为我需要从上面的代码中获取的唯一行是:

req.headers['my_key'] = 'my_value'

但不确定我是否需要 req.pipe 行。

在 npm 上至少使用一个中间件来处理 Express 中的 CORS:[请参阅@mscdex 回答]

将 header 字段设置为值

res.set('Content-Type', 'text/plain');

或传递 object 一次设置多个字段。

res.set({
  'Content-Type': 'text/plain',
  'Content-Length': '456'
})

别名为

res.header(field, [value])

有关详细信息,请阅读 Express documentation