I have an Express route /doc/:id which serves up the HTML representation of a document, and I want it to serve the EPUB representation when appended with ".epub". Express doesn't separate on a period, however, so if I use /doc/:id.epub sets req.params.id to "id.epub". Is there a way to have the file extension recognised as a separate parameter or do I simply need to use a regex to extract it?
I have looked at res.format, but it seems this is only effective when the Accepted header is set, which it would not be if the URL is simply typed into a browser, as far as I can see.
This works:
app.get('/doc/:filename.:ext', function(req, res) {
...
});
This requires that the part following /doc/ contains at least one period, which may or may not be an issue.
Based on the behavior you are describing, I think you might have your route matching rules out of order.
This works:
app.get('/doc/:id.epub', function(req, res, next){
res.send('id: ' + req.params.id); //match /doc/x.epub (id=x)
});
app.get('/doc/:id', function(req, res, next){
res.send('id: ' + req.params.id); //match doc/x (id=x) | doc/y.html (id=y.html)
});
If /doc:/:id is first you'd get an id of x.epub for /doc/x.epub.