Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable etag Header in Express Node.js

We have a need to remove the etag header from all HTTP responses in our Node.js Express application. We have a web services API written in Express where unexpected results are seen at the client when we send etags and the client sends back the if-none-match header.

We've tried app.disable('etag') and res.removeHeader('etag'), but neither work; the app sends the header regardless.

Is there any other means of disabling this header in all responses?

like image 995
Raj Avatar asked Mar 03 '13 22:03

Raj


People also ask

What is ETag Express?

ETag value is nothing crazy, but a non-reversible, short, and fast hashed value of the Response Data (body). Server sends ETag header in Response to Client. Client sends if-none-matched header (with its value being previously received Etag values from server) in Request to Server.

What is the ETag header?

An ETag (entity tag) is an HTTP header that is used to validate that the client (such as a mobile device) has the most recent version of a record. When a GET request is made, the ETag is returned as a response header. The ETag also allows the client to make conditional requests.


2 Answers

app.disable('etag') should work now, there has been a pull request merged to deal with this:

https://github.com/visionmedia/express/commit/610e172fcf9306bd5812bb2bae8904c23e0e8043

UPDATE: as Bigood pointed out in the comments the new way of doing things is the following

app.set('etag', false); // turn off 

Change occured with version 3.9.0: https://github.com/strongloop/express/releases/tag/3.9.0

For more options on setting the etag check the 4.x docs here: http://expressjs.com/4x/api.html#app.set

like image 194
alessioalex Avatar answered Sep 20 '22 06:09

alessioalex


app.disable('etag') 

This will disable etag header for all requests, but not for static contents. The following does it for static content:

app.use(express.static(path.join(__dirname, 'public'), {         etag: false })); 
like image 40
PapaKai Avatar answered Sep 20 '22 06:09

PapaKai