Take the 2-minute tour ×

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?

share|improve this question
Are you meaning to use :path? –  Joachim Isaksson Aug 20 '13 at 18:40
@JoachimIsaksson: If the user will go to app/static/a/b/c.html and the route is /app/static/:path, will path be equal to "a/b/c.html"? –  Naor Aug 20 '13 at 18:51
add comment

3 Answers

up vote 2 down vote accepted

Just us 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'));
share|improve this answer
I already define static folder. Can I declare another one?.. –  Naor Aug 21 '13 at 5:54
Yes, you can have however many you want, just have additional app.use calls. –  Peter Lyons Aug 21 '13 at 7:17
add comment

If you want to include subdirectories, you can do it using a regex; this regex will match any directory/file structure under /app/static/

app.get(/^\/app\/static\/(.*)/, function (req, res) {
    console.log('assets/app/static/' + req.params[0]);
    res.sendfile('assets/app/static/' + req.params[0]);

As to your question about multiple static directories, yes, you can. Just app.use both;

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

The file will be served from the first directory where it's found (searching /assets first, then /alternate_assets), but to make things less confusing, you may want to avoid having the same file name in both directories.

share|improve this answer
add comment

None of the other answers had worked for me in the past because they were missing two small details:

  • app.use calls to define static directories should be async (e.g. wrapped in a function), or they will try to execute before the websocket has finished establishing its connection (in my opinion, this is an issue with Connect -- the framework which Express is built on top of; I should be able to just start writing my code at the top of the file).

  • These definitions need to happen before you send the HTML file that would have references to the URL, within that (async) function body.

Like this:

  res.sendfile(__dirname + '/index.html'); 

And then you can include them in directly in your HTML (or SVG, etc.) like this:

<link rel="stylesheet" href="static/index.css">

<image x="0" y="0" xlink:href="static/picture.svg" />

• etc.

share|improve this answer
add comment

Your Answer


