Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Removing all headers from express.js

Removing all headers from express.js

Problem

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.

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
Problem courtesy of: Jonny Flowers

Solution

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.

The Date header is such a required one. See here: 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.

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);
});
Solution courtesy of: Slavo

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Removing all headers from express.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×