Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS routing vs backend routing

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?

like image 459
anti Avatar asked Mar 21 '13 21:03

anti


People also ask

What is backend routing?

Each request to the backend is eventually executed by a controller. A list of routes is defined which maps a given request to a controller and an action. Routes are defined inside extensions, in file Configuration/Backend/Routes.

What is AngularJS routing?

Routing in AngularJS is used when the user wants to navigate to different pages in an application but still wants it to be a single page application. AngularJS routes enable the user to create different URLs for different content in an application.

What is routing in AngularJS and how does it work?

Routing in AngularJS is a method that allows you to create Single Page Applications. It enables you to create different URLs for different content in your web applications. AngularJS routing also helps to show multiple contents depending on which route is chosen. It is specified in the URL after the # sign.

What is meant by Angular routing?

Angular RoutinglinkIn a single-page app, you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page. As users perform application tasks, they need to move between the different views that you have defined.


2 Answers

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.

like image 122
hotienvu Avatar answered Oct 05 '22 07:10

hotienvu


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.

like image 24
holographic-principle Avatar answered Oct 05 '22 05:10

holographic-principle