Naor Naor - 1 year ago 40
Node.js Question

node js express route all paths

I am using express. I like to create a route that navigates all requests of type "get" with url prefix '/app/static/*pagePath' to "/assets/app/static/pagePath".
I am trying to do the following, but is doesn't work.

app.get('/app/static/*path', function (req, res) {
res.sendfile('assets/app/static/' + req.params.path);

Any idea?


Just use a middleware with a prefix and some short-circuit logic:

app.use('/app/static', function (req, res, next) {
  if (req.method !== 'get') {
  res.sendFile(__dirname + '/assets' + req.path);

(this is untested so might not be 100% ready to go, but you get the idea)

Actually, looking again at your question, are you sure this can't be handled by the express.static middleware just given the proper root directory?

app.use('/app/static', express.static(__dirname + '/assets'));