简体   繁体   English

如何在Node.js中使用相同的密钥设置多个http头字段?

[英]How do I set multiple http header fields with the same key in Node.js?

I'm trying to set up server push with cloudflare , but they require multiple link header fields to push multiple files. 我正在尝试使用cloudflare设置服务器推送 ,但是它们需要多个link头字段来推送多个文件。 However, I can't find any documented way to include multiple header fields with the same key in node.js. 但是,我找不到任何记录的方法来在node.js中包含具有相同键的多个头字段。 I tried providing an array, but that just concatenates them together as the value for a single header field. 我尝试提供一个数组,但这只是将它们连接在一起作为单个头字段的值。

express 表达

You pass an array of values to res.header('HeaderName', arrayOfValues) . 您将一组值传递给res.header('HeaderName', arrayOfValues) Here's a working example and cURL output showing the duplicate response headers. 这是一个工作示例和cURL输出,显示重复的响应标头。 This is not directly documented, but it does work (express@4.14.0). 这不是直接记录,但确实有效(express@4.14.0)。

const express = require('express')
const app = express()
app.get('/', (req, res, next) => {
  res.header('Link', ['Link1', 'Link2'])
  res.send()
})
app.listen(3000)

curl -v localhost:3000 output: curl -v localhost:3000输出:

< HTTP/1.1 200 OK
< X-Powered-By: Express
< Link: Link1
< Link: Link2
< Date: Fri, 09 Sep 2016 01:44:22 GMT
< Connection: keep-alive
< Content-Length: 0

node core http 节点核心http

Use res.setHeader(name, arrayOfValues) 使用res.setHeader(name,arrayOfValues)

const http = require('http')

const server = http.createServer(function (req, res) {
  res.setHeader('Link', ['Link1b', 'Link2b'])
  res.end()
})
server.listen(3000)

curl output: 卷曲输出:

< HTTP/1.1 200 OK
< Link: Link1b
< Link: Link2b
< Date: Fri, 09 Sep 2016 01:52:53 GMT
< Connection: keep-alive
< Content-Length: 0

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

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