0

I have an Angular/Node/Express webapp built on generator-angular-fullstack.

In development mode, the app works just fine. But in production mode, it serves index.html whenever static assets (images, CSS) are requested, and I can't figure out why - I guess something is wrong with my ExpressJS routing.

lib/express.js: set path for static assets

// Mode: Production  
app.configure('production', function(){
    app.use(express.favicon(path.join(config.root, 'public', 'favicon.ico')));
    app.use(express.static(path.join(config.root, 'public')));
    app.set('views', config.root + '/views');
});

lib/routes.js:

module.exports = function(app) {
    // Server API Routes
    app.get('/api/awesomeThings', api.awesomeThings);

    app.post('/api/users', users.create);
    app.put('/api/users', users.changePassword);
    app.get('/api/users/me', users.me);
    app.get('/api/users/:id', users.show);

    app.post('/api/session', session.login);
    app.del('/api/session', session.logout);

    // All other routes to use Angular routing in app/scripts/app.js
    app.get('/partials/*', index.partials); // index.partials defined in controllers/index.js
    app.get('/common/*', index.partials);
    app.get('/ui-editor/*', index.partials);

    app.get('/*', middleware.setUserCookie, function(req, res) {
        res.render('index');
    });
};
Tom Söderlund
  • 4,743
  • 4
  • 45
  • 67
  • My guess is that your config.root value is incorrect when running in production and as a result it cannot find any of your static files and therefore ther request is handled by your /* route – Hector Correa Jan 21 '14 at 15:11
  • @HectorCorrea Thanks but it doesn't look like it: `config.root` is `[MY_APP_PATH]` in development mode, and `[MY_APP_PATH]/dist` in production mode which looks correct. – Tom Söderlund Jan 21 '14 at 16:11
  • Turned out the problem was not in routing, but in my Gruntfile renaming assets wrongly. – Tom Söderlund Jan 23 '14 at 11:18
  • **Clarification:** the `rev` step in my Gruntfile renamed my images/CSS to unexpected filenames. The Express app served index.html correctly (see `app.get('/*', ...)` above), since I was asking for the wrong filename. Solution: I disabled `rev` in my Gruntfile. – Tom Söderlund Mar 24 '14 at 21:19

0 Answers0