1

I'm using expressjs to run my server and have set up the default route for static content as follows: app.use(express.static(path.join(__dirname.substr(0, __dirname.lastIndexOf('/')), 'public')));, where all my static assets are found in public/src, public/styles etc...

Now I want to do a simple find and replace on in public/index.html but as it is in my static folder it ignores any routes I subsequently set up for app.get('/, function ....

Is there a way I can apply some preprocessing to index.html without having to move all my other static files into a subdirectory e.g. can express.static() be passed an ignore list somehow?

wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • try putting the static middleware after your routes, also app.get on / is not the same as app.get on /index.html – r043v Sep 23 '13 at 12:27
  • @r043v - Doesn't work unfortunately, trying lots of different orders, both within and without `app.configure()` – wheresrhys Sep 23 '13 at 13:08
  • try with http://pastebin.com/JVdu6XBt before it work, after it's not – r043v Sep 23 '13 at 15:00
  • I also have `app.get('/*', function (req, res, next){ res.writeHead(301, { Location: '/' }); res.end(); });`, (temporary disabling of deep linking) which it turns out is a complicating factor – wheresrhys Sep 23 '13 at 16:07

1 Answers1

1

Set up your app to use your routes before using static:

app.use(app.router);  // Make sure this gets called *before* the next line
app.use(express.static(...));
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
  • the static routes don't seem to take effect unless they're inside an app.configure( ...), which I assume always gets applied before the routes. If I put my other routes inside configure( ... ) then they fail instead. – wheresrhys Sep 23 '13 at 16:53
  • Are you explicitly calling `app.use(app.router)` anywhere? When you do `app.get(...)` or `app.post(...)` routes, it gets called behind the scenes, but you should be able to ensure that your routes get called before `static` if you call it yourself. – Trevor Dixon Sep 23 '13 at 17:25
  • Fter reading that I thin kmy problem is I want my /|/index.html route attempted first, then my statics, then my /* route (for disabling deep linking). I guess I just need to use a less aggressive path string than '/*' – wheresrhys Sep 24 '13 at 08:15