Question

Trying to understand how to implement AngularJS in a node.js express app. After setting up express, I need 2 things: routing and a template engine, so normally I would need to do as follows to set the app to use Jade templating engine:

 app.register('.html', require('jade'));

...and then I would set routes probably like this:

app.get('/', function(req, res) {
   res.render('index', function(err, html){
      // ...
   });
});

But if I want to use AngularJS for templating, do I still need Jade? And I read about how in AngularJS routes must be configured, does this mean the above way of declaring routes with app.get() would no longer be needed when using AngularJS?

Was it helpful?

Solution

If you don't need to add anything extra to your Angular layout prior to rendering the page for the client (i.e. in some cases you could add a window.user object in the Jade template for authentication when using PassportJS), you can completely ditch Jade altogether and let the Express static middleware render your index.html:

app.use(express.static(path.join(__dirname, 'public')));

Obviously, the files in public/ are all your Angular files, including the index.html. Be sure to require the path module too for path normalization, this isn't required though.

Afterwards, Angular will take care of the rest. This means that all your routes are defined inside the Angular app, and not in the Express routes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top