[英]How to set a HTTP Header Key/Value Pair within the MEAN Stack
I'm using the MEAN stack and need to set a HTTP header key/value pair within my web app. 我正在使用MEAN堆栈,需要在我的Web应用程序中设置HTTP标头键/值对。
ie my_key: my_value
即
my_key: my_value
Using Angular 5 and found the following code: 使用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)
})
but unsure how to apply to my requirement as I believe the only line that I need from the above code is: 但不确定如何应用于我的要求,因为我认为上述代码中唯一需要的一行是:
req.headers['my_key'] = 'my_value'
but not sure if I need the req.pipe
line. 但不确定是否需要
req.pipe
行。
Use at least one middleware on npm for handling CORS in Express: [see @mscdex answer] 在npm上至少使用一种中间件来处理Express中的CORS:[请参阅@mscdex答案]
Set header field to value 将标题字段设置为值
res.set('Content-Type', 'text/plain');
or pass an object to set multiple fields at once. 或传递一个对象以一次设置多个字段。
res.set({
'Content-Type': 'text/plain',
'Content-Length': '456'
})
Aliased as 别名为
res.header(field, [value])
for more information read the Express documentation 有关更多信息,请阅读Express文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.