从express.js删除所有标头 [英] Removing all headers from express.js

查看:54
本文介绍了从express.js删除所有标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个页面,其中有一些数据已由其他设备解析.我曾经用php来做到这一点,但是我将其移到了node.我需要从页面中删除所有标头,所以我只有输出.此输出是对GET请求的响应.

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.

此刻我有

HTTP/1.1 200 OK
Date: Wed, 11 Sep 2013 11:54:14 GMT
Connection: close

My output

我需要它才能显示

My output

推荐答案

通常,您可以使用Express(node.js)中的Response对象的API删除标头,但是,HTTP规范要求其中一些标头并且应该一直在那里.

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.

Date标头是必需的.看到这里: https://stackoverflow.com/a/14490432/1801

The Date header is such a required one. See here: https://stackoverflow.com/a/14490432/1801

第一行( HTTP/1.1 200 OK )不是标头-它是HTTP协议的一部分,每个响应都应以此开头.否则,浏览器将不知道如何处理响应.

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.

如果要删除其他自定义标头,则可以这样做:

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);
});

这篇关于从express.js删除所有标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆