简体   繁体   中英

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.

ie my_key: my_value

Using Angular 5 and found the following code:

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.

Use at least one middleware on npm for handling CORS in Express: [see @mscdex answer]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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