Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 799
Jonny Flowers Avatar asked Sep 11 '13 11:09

Jonny Flowers


People also ask

How to remove header in node js?

removeHeader() (Added in v0. 9.3) property is an inbuilt property of the 'http' module which removes a header identified by a name that's queued for implicit sending.

How do I remove a header request?

Guidelines. The dp:remove-http-request-header function removes a specific header field and its associated value from the protocol header of a client request. If the client request contains the header field that is identified by the name parameter, the function removes this header field from the client request.

Is Express JS unmaintained?

It is unmaintained Express has not been updated for years, and its next version has been in alpha for 6 years. People may think it is not updated because the API is stable and does not need change. The reality is: Express does not know how to handle async/await .

What is headers in node JS?

The header tells the server details about the request such as what type of data the client, user, or request wants in the response. Type can be html , text , JSON , cookies or others.


2 Answers

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

like image 199
Hiren Patel Avatar answered Oct 08 '22 13:10

Hiren Patel


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);
});
like image 40
Slavo Avatar answered Oct 08 '22 13:10

Slavo