0

So I'm learning how to develop a web using angular and node JS and I am confused as to where to decide where acquire the URLs for links. I have been reading around stack overflow as well as random tutorials on both angular and node, and most of them just explain the route handler (which I have figured out now). Generally, they suggest that angular should handle all page routing and so I assume that they're also supposed to handle URL generation. But I read somewhere that the web server(node?) should be generating the URL's and that angular should only be implementing route handling given the URLs. I have no clue how I should be doing it (I want to do it right the first time so I don't end up refactoring a lot of code) and I am quite confused about how to go about generating the URLs that I need. Any clarification would be greatly appreciated. Thanks in advance!

atse
  • 3
  • 2

2 Answers2

2

For a Single Page App (Angulars bread and butter) it's quite common to have a single server side route handler that will serve the exact same content regardless of what page the user lands on. At that point Angular can take over and work out what controller/view is required (via route handler) and show it there.

Corroborating answer - not in node sorry - How to use ASP.NET MVC and AngularJS routing?

Now, when I navigate to http://www.example.com/Application1/view[x] it routes to the Application1 MVC controller, Index action, and then Angularjs takes over routes to the different views, all while having the HTML5 routing construct in the URL.

Community
  • 1
  • 1
Dave Walker
  • 3,498
  • 1
  • 24
  • 25
  • So the controllers will handle URL generation and the route handler will redirect it to the appropriate template? I thought that it was best practice to have the web server generate the URLs for the route handler to parse. – atse Nov 19 '14 at 19:44
  • Well your server side is going to have to handle it in some regard. Given that you want to have the client side handling routing when acting as a SPA - as in once a client is on the page and they are interacting with the site - then it makes sense to put all of the routing logic into there. – Dave Walker Nov 19 '14 at 19:51
  • Also if you're wanting to create some form of sitemap then you may need a server side representation of the url as well.. But that's out of scope for your question. – Dave Walker Nov 19 '14 at 19:52
  • So right now, on my backend (node), I have a route that serves my index.html to any requests made to the server and express hosts all the public files (angularjs, css and html for fronted). My route handler is currently configured to serve a specific template into my ngview based on the URL from certain button clicks (through hrefs). Is that right? – atse Nov 19 '14 at 19:56
  • So should I create a sitemap on the server side and then make the request from the angular end to get the next link? – atse Nov 19 '14 at 19:57
  • I shouldn't have mentioned sitemaps it's going to confuse this. Your previous comment seems right. NB you can set your backend up to serve dynamic templates into your angular app as well. – Dave Walker Nov 19 '14 at 20:20
0

You'll need both anyways, so don't worry about doing too much work. The question is HOW you do it and what your app is like. The parts that are public or should be accessed by search engines, should probably be routed by both node and angular.

If you have a part of the site that is more like a web application behind a login, you can in some cases rely on letting angular do the routing work (mostly) you still need endpoints in your node.js routes that give you back the data (probably JSON).

TLDR: public: both, HTML from node private: angular, JSON from node