I have a route that serves an item from mongoDB. I don't know how to use the conditional plugin with restify to make sure restify sends a 304 Not Modified header if the Etag matches or modified date is older than the retrieved date. How can i get this to work?
The example on the homepage of restify seems not to work, in the sense it doesn't send a 304 header and neither does it call next();
This is a pretty fundamental use case in my opinion.
Yeah, the conditionRequest plugin isn't documented well. Here's an example use:
server.get('/:example', function setETag(req, res, next) {
var etag = something.getETag(req);
res.setHeader('ETag', etag);
return next();
}, restify.conditionalRequest(), function getPayload(req, res, next) {
var payload = something.getPayload(req);
res.send(payload);
return next();
});
Hi @fnogatz, do you mind clarifying the example for me? My naive etag implementation would be to MD5 the response payload, however in your example you calculate the etag from the request only?
It's right though that the conditionalRequest middleware doesn't rely on res.once('header'), so it seems it will always apply before the response is calculated.
I stopped using restify altogether, in my opinion express is much superior than restify and the performance of express greatly surpasses restify in the recent releases. Express does this by default, you don't need to do anything for 304 headers and etag :)
Out-of-the-box support for etags would definitely be great.
We had a PR for that, but did not make it way into the master. Would be nice if someone could finish it: https://github.com/mcavage/node-restify/pull/536
Can you please provide an example to set etag and compute etag value from body response ?