【问题标题】:How to set a HTTP Header Key/Value Pair within the MEAN Stack如何在 MEAN 堆栈中设置 HTTP 标头键/值对
【发布时间】:2018-10-18 12:15:54
【问题描述】:

我正在使用 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 行。

【问题讨论】:

    标签: javascript angular express http-headers mean-stack


    【解决方案1】:

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

    将标题字段设置为值

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

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

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

    别名

    res.header(field, [value])
    

    更多信息请阅读Express documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-25
      • 2013-12-24
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 2015-03-17
      • 2016-12-03
      • 1970-01-01
      相关资源
      最近更新 更多