Search code examples
javascriptnode.jsexpressbackbone-boilerplate

express route to different file


I am new in express and I am using Backbone Boilerplate also. In case of development, when asking for /assets/css/index.css I want to deliver /public/dist/debug/index.css.
I've made this:

var env = process.env.NODE_ENV || 'development';
switch (env) {
    case 'development':
        app.get('/assets/css/index.css', function(req, res) {
            res.sendfile('public/dist/debug/index.css');
        });
        break;
}

But for some reason my page keep getting the incorrect file: /assets/css/index.css.

What is wrong?


Solution

  • It should work, unless you use express.static() (which I assume is handling the requests for /assets/css/index.css; if not, replace with 'the route that's handling those requests' :) before your route (which would mean that the static middleware would get to handle the request first).

    Also, instead of your switch statement, you can use app.configure:

    app.configure('development', function() {
      // this code will only run when in development mode
      app.get('/assets/css/index.css', function(req, res) {
        res.sendfile('public/dist/debug/index.css');
      });
    });
    
    // static middleware after your route
    app.use(express.static(...));