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?
app.disable('etag') should work now, there has been a pull request merged to deal with this:
https://github.com/visionmedia/express/commit/610e172fcf9306bd5812bb2bae8904c23e0e8043
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
}));
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.
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.