Raj
Raj

Reputation: 3908

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?

Upvotes: 43

Views: 42242

Answers (6)

Kevin Danikowski
Kevin Danikowski

Reputation: 5196

You Can't using express

You can only make them false, but that will still result in a 304 redirect because your etag will match the last request (which returned false)

I used this solution from another npm package (https://github.com/helmetjs/nocache/issues/9) which uses on-headers npm package to modify the header after express adds the etag.

This can be added anywhere you have your res being called, for a specific route or all routes:

const onHeaders = require('on-headers');

onHeaders(res, function () {
    this.removeHeader('etag');
});

Note, you may also need to change your cache settings. But this will remove your express etags :)

Upvotes: 1

Lukas Liesis
Lukas Liesis

Reputation: 26403

This sample works perfectly.

  1. No undocumented hacks
  2. Fine tuning for each endpoint
  3. Can add/remove any header :)

    var onHeaders = require('on-headers')
    
    function myRoute(req, res) {
      scrubETag(res)
      // do the rest
      res.send('something')
    }
    
    function scrubETag(res) {
      onHeaders(res, function () {
        this.removeHeader('ETag')
      })
    }
    

Source: https://github.com/expressjs/express/issues/2472

Upvotes: 3

alessioalex
alessioalex

Reputation: 63663

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

Upvotes: 70

PapaKai
PapaKai

Reputation: 444

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

Upvotes: 29

Tony
Tony

Reputation: 55

Looking at the express response.js, Etags are only sent when the request method is GET. You can prevent express from sending etags in the response by setting the request.method to something else before calling response.send().

eg:

app.get('/path/returnsJSON', function(req, res){
/* HACK to workaround the framework sending e-tags and "304 NOT MODIFIED" responses */
req.method="NONE"; // was "GET"
res.status(200).send({data:1}); 
});

This worked OK for me.

Upvotes: 3

loganfsmyth
loganfsmyth

Reputation: 161457

It seems to me that the real solution to your problem would be to figure out why it is behaving strangely due to the etags.

To answer your question though, Express does not currently have support for disabling the etags headers. It was actually discussed and merged in this pull request, but was later reverted. If you really need this and don't want to try to fix the root problem, you can always apply that patch and go from there.

Upvotes: 2

Related Questions