Jonny Flowers
Jonny Flowers

Reputation: 631

Removing all headers from express.js

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

Upvotes: 11

Views: 35863

Answers (4)

Hiren Patel
Hiren Patel

Reputation: 51

app.use(function (req, res, next) {
    res.removeHeader("x-powered-by");
    res.removeHeader("set-cookie");
    res.removeHeader("Date");
    res.removeHeader("Connection");
     next();
});

For more details Please look into this doc for best way to explain all the http header related query:: Express (node.js) http header docs

Upvotes: 5

Mark Stosberg
Mark Stosberg

Reputation: 13381

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.

Upvotes: 0

Mike Cooper
Mike Cooper

Reputation: 3038

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.

Upvotes: 1

Slavo
Slavo

Reputation: 15463

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

Upvotes: 25

Related Questions