0

In our Express middleware, we have a call to:

 app.enable('etag');

the problem is that for JSON requests, the client will get a 304 status code for JSON requests that should always be re-evaluated/re-sent.

So my question is, using Express middleware, how can I enable etag for static assets, but disable it for JSON requests?

I'd probably have to inspect the headers for each request and enable/disable etags per request?

1 Answer 1

0

So in production, it seems like this will work:

app.enable('etag');

app.use("/public", express.static(path.join(__dirname, "..", "public")));

app.use(function (req, res, next) {
  // we don't want to cache any JSON response
  res.setHeader('Cache-Control', 'no-cache, no-store');
  next();
});

app.use(function(req,res){
   res.json({some:'this will never be cached'});
});

I think what this will accomplish is allow client caching of the static assets, but it will never cache any of the JSON responses in the client.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.