繁体   English   中英

如何在MEAN堆栈中设置HTTP标头键/值对

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

我正在使用MEAN堆栈,需要在我的Web应用程序中设置HTTP标头键/值对。

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答案]

将标题字段设置为值

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

或传递一个对象以一次设置多个字段。

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

别名为

res.header(field, [value])

有关更多信息,请阅读Express文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM