Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting filetype in an Express route

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.

like image 224
tsvallender Avatar asked Nov 16 '25 10:11

tsvallender


1 Answers

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.

like image 174
robertklep Avatar answered Nov 18 '25 17:11

robertklep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!