如何在Node.js中使用相同的键设置多个http标头字段? [英] How do I set multiple http header fields with the same key in Node.js?
问题描述
我正在尝试设置服务器推送使用cloudflare ,但它们需要多个 link
标头字段来推送多个文件.但是,我找不到任何记录的方法来在node.js中包含具有相同键的多个标头字段.我尝试提供一个数组,但这只是将它们串联在一起作为单个标头字段的值.
I'm trying to set up server push with cloudflare, but they require multiple link
header fields to push multiple files. However, I can't find any documented way to include multiple header fields with the same key in node.js. I tried providing an array, but that just concatenates them together as the value for a single header field.
推荐答案
表达
您将值数组传递给 res.header('HeaderName',arrayOfValues)
.这是一个工作示例,并且cURL输出显示了重复的响应标头.它没有直接记录下来,但确实可以使用(express@4.14.0).
express
You pass an array of values to res.header('HeaderName', arrayOfValues)
. Here's a working example and cURL output showing the duplicate response headers. This is not directly documented, but it does work (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输出:
curl -v localhost:3000 output:
< 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
节点核心http
使用 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)
卷曲输出:
< HTTP/1.1 200 OK
< Link: Link1b
< Link: Link2b
< Date: Fri, 09 Sep 2016 01:52:53 GMT
< Connection: keep-alive
< Content-Length: 0
这篇关于如何在Node.js中使用相同的键设置多个http标头字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!