AngularJS routing vs backend routing

All we need is an easy explanation of the problem, so here it is.

I would like to use AngularJS in my next project.
The application with Python backend and html5, Angular frontend.

I am going to use MVC framework on backend and I am little bit confused.
Do I have to use routing on backend and also frontend?

Because I always used backend routing and routing on frontend is really new idea for me.

Is client side routing better? And when I choose to use frontend routing, there will be no routes on backend? All request will be send to one url?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can use client-side routing and let the backend return static files and JSON data.
The routing in Angular basically tell you which partial template you need to download from server and which controller will handler it.

Your back end routes will be like this

'/partials/:name' -> return corresponding partial

'/api/*' -> handlers to return json data

'/*' -> return index.html

Your index.html will contain reference to other views

...
<div ng-view></div>
...

Now let say you go to yourapp.com/someview.html. The server returns
index.html and since the url is /someview, Angular will ask for “someview” partial from server and render the page accordingly.

In short, the server role is to return index.html, partials and serve REST API request. The client always receive index.html and based on the url, request for coressponding partials and JSON data.

Method 2

You can choose to do either client-side routing, server-side routing or a combination of both…

In case of client-side routing, you would have a single page served by the server. All routing after that would be done by the client. If you have an application with a lot of pages, this might not be the optimal solution and you might want to do server-side routing.

In case of server-side routing, you would serve a page for every route defined on the server application. Each of them would contain a mini-angularJS application (furthermore, each of these mini-SPAs could do some additional routing if that makes sense)

It’s up to you to decide what works best for your case.

Update:

Definitely check out UI-router from the AngularUI project if you are planning to do client-side routing. It gives you the ability to create state-based views and some other things that were not possible with Angular’s native routing.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply