2

Both Node.js and AngularJS seem capable of handling a lot of the same things, and I'm not sure how I should delegate tasks between the two frameworks.

For example, to render "localhost:3000/foo" in a browser you could either write this in express:

router.get("/foo", function (req, res, next) {
  res.render("bar", callback);
});

or you could write a partial and include this block in config:

when("/foo", {
  templateUrl: "views/partials/bar.html",
  controller: "FooCtrl"
}).

In more general terms: what tasks should I delegate to AngularJS, and what should I leave to express?

Thanks in advance!

1
  • 1
    Use Angular for routing if you have a small app and want to keep everything in one place. Use Node for routing if you need to make server side business logic decisions (like, returning something different for / depending on if the user is logged in or not.) If you want to do everything in angular, then use [http-server](https://www.npmjs.com/package/http-server) instead of express. Commented Aug 3, 2015 at 20:03

1 Answer 1

3

While both Angular and Express can handle routing for you, they both do it in very different ways.

If you handle routing on the backend (using Express), then with each click, the browser would go to your backend and you will have to render the complete HTML page on the server and send it back to the browser.

However, if you handle routing using HTML5 routing on the client (using ng-route), you can avoid that call to the backend (if, say, you include a template instead of a templateURL in your route definition). Then you will have a Single Page Application (SPJ) which will only need to make REST API calls to your backend. The rendering (usually) in then handled on the client fully.


Pros of server based routing (using Express)

  • All code resides on the backend. Less fragmentation.
  • On clients with low computation power, the server can do the heavy lifting
  • Old browsers may have problem handling advanced Javascript (or may be too slow) but they can usually handle HTML + some light scripts. This is especially true if you want to target browsers which do not support the HTML5 routing API. In that case, you'll have to resort to using hashtag based URLs.

Pros of client based routing (using AngularJS)

  • The server has to handle rendering which can tax the servers computationally and bandwidth wise. Offloading this task to the client can make sense if it is not too taxing.
  • If the client code can be separated from the backend code, it makes the front-end independent (except for the coupling at the API) from the backend. This can allow more flexibility in iterations if different teams are working on them.
  • Client side rendering can appear smoother if done properly. For example, each user action can only update parts of the page instead of refreshing the whole page.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.