AngularJS with Express Templates or pure HTML? Pros and Cons?

Sam Fast picture Sam Fast · Dec 13, 2013 · Viewed 10k times · Source

Express JS uses templates for generating HTML and then server sends them to client in response. There may be several other templates from which HTML can be generated. The ones I was able to discover are:

In my app, I need to use both ExpressJS and AngularJs. I am new to both technologies. While learning angular, I had to use it in pure HTML. After learning ExpressJs, I realized, in order to use angularjs, I need to use them in any of the above templates which will be converted to HTML while sending to client.

Now, I want to use expressjs as my server and angularjs as my client side app. For this, I think I have two options.

Option 1

I can stop using templates altogether and use our NodeJS server to respond by sending simple HTML files. These HTML files will then contain AngularJS coding within them. AngularJS then, on client side, will act as our application. It will demand other HTML documents from the server. Or it can also be used like AJAX, where we can only request the piece of information to update just part of the page rather refreshing the whole page for a minor change.

Option 2

I can use angularjs inside expressjs templates (jade or ejs).

Kindly, help me in understanding the pros and cons of both options. Which one will be your choice in such case.

Answer

John Munsch picture John Munsch · Dec 13, 2013

This is very much an opinion question and Stack Overflow admins hate anything that smacks of opinion, but here's my experience and opinion nevertheless.

I've done a couple of apps now using purely static files (HTML, CSS, and JavaScript) with those calling a service on the back-end to deliver the data. It reduces the back-end, whatever it is (I've used both Java and Node.js), to just being a set of service URLs but it works very very well.

  • You've got a fantastic hard line between the responsibilities of the two systems
  • It's very easy to work on and test each one independently
  • Bugs are usually very clearly in the front-end or the back-end (all you have to do is look at the data transferred to know)
  • The back-end services are ready to be reused to support alternative UIs from the command line or something mobile specific if you want
  • You can use one technology for the back-end to start with (say Node.js or Ruby on Rails) and then switch to something else later if you need to. As long as the API stays the same the front-end never knows.