javascript 从 express.js 中删除所有标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18740504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing all headers from express.js
提问by Jonny Flowers
I am creating a page where I have some data that gets parsed by another device. I used to do this with php but I am moving it to node. I need to remove any and all headers from the page so I only have my output. This output is a response to a GET request.
我正在创建一个页面,其中有一些数据会被另一台设备解析。我曾经用 php 做到这一点,但我把它移到了 node.js 上。我需要从页面中删除任何和所有标题,所以我只有我的输出。此输出是对 GET 请求的响应。
At the moment I have
目前我有
HTTP/1.1 200 OK
Date: Wed, 11 Sep 2013 11:54:14 GMT
Connection: close
My output
I need it to just display
我需要它来显示
My output
回答by Slavo
Generally, you can use the API of the Response object in Express (node.js) to remove headers, however, some of them are required by the HTTP spec and should always be there.
通常,您可以使用 Express (node.js) 中 Response 对象的 API 来删除标头,但是,其中一些标头是 HTTP 规范所要求的,并且应该始终存在。
The Date header is such a required one. See here: https://stackoverflow.com/a/14490432/1801
Date 标头就是这样一个必需的标头。见这里:https: //stackoverflow.com/a/14490432/1801
The first line (HTTP/1.1 200 OK
) is not a header - it is part of the HTTP protocol and each response should start with it. Otherwise the browser wouldn't know what to do with the response.
第一行 ( HTTP/1.1 200 OK
) 不是标头 - 它是 HTTP 协议的一部分,每个响应都应以它开头。否则浏览器将不知道如何处理响应。
If you want to remove other custom headers, you can do it like this:
如果要删除其他自定义标题,可以这样做:
app.get('/test', function (req, res) {
var body = "some body";
res.removeHeader('Transfer-Encoding');
res.removeHeader('X-Powered-By');
res.end(body);
});
回答by Mike Cooper
Express isn't going to do this, since Express is for HTTP. What you have asked for is not HTTP, as it does not follow some of the RFCs. To do what you want, you would have to bypass express. Listen on the port, parse the GEt request from the embedded device, and send the data that you want.
Express 不会这样做,因为 Express 是用于 HTTP 的。您要求的不是 HTTP,因为它不遵循某些 RFC。要做你想做的事,你必须绕过快递。侦听端口,解析来自嵌入式设备的 GEt 请求,并发送您想要的数据。
回答by Mark Stosberg
If the code on the other device can be changed, a more standard solution would be for the the device to ignore the HTTP headers and just parse the body.
如果可以更改其他设备上的代码,则更标准的解决方案是设备忽略 HTTP 标头并仅解析正文。