我正在使用 MEAN 堆栈,需要在我的 Web 应用程序中设置 HTTP header 键/值对。
即my_key: my_value
使用 Angular 5 并发现以下代码:
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');
或传递一个对象来一次设置多个字段。
res.set({
'Content-Type': 'text/plain',
'Content-Length': '456'
})
别名为
res.header(field, [value])
有关更多信息,请阅读 Express documentation
关于javascript - 如何在 MEAN 堆栈中设置 HTTP header 键/值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52873814/