Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AngularJS be used without a REST API?

When I am creating a simple website with node.js I am fine with using the view engine (eg. jade) and controllers that provide data to it (eg. simple todo list). However, if I decide to add AngularJS as the client framework then it seems that I must implement REST API on the backend to get data from it. Almost all examples I see online with AngularJS have this basic architecture: client (angular) communicates with the server via REST API.

Can AngularJS be used without REST API and if so should I do it or should avoid it? Are there any recommendation/best practices for using AngularJS without REST API backend?

like image 515
matori82 Avatar asked Jun 05 '15 22:06

matori82


1 Answers

Absolutely. Angular can still do a lot on your site even if you never utilize the $http service to talk to your server. You can still take advantage of the utilities for helping out with managing your DOM.

That said, most modern apps need to get data from the server. There are tons of reasons why you might need to do this. For example, if you had users that needed to sign up then you'd need to store their username and password somewhere. That somewhere would be in a database that only your server can access. Then your server would provide some URLs that you can talk to via Angular's $http service.

If you do have an app that makes calls to the server but you want to turn off the network communication for testing, you can mock the $http call responses. Angular provides an $httpBackend for that exact purpose. You can use it to set up dummy URLs that pretend to respond to your $http calls so that your $http calls don't know they aren't actually talking to a server.

authRequestHandler = $httpBackend.when('GET', '/auth.py')
                       .respond({userId: 'userX'}, {'A-Token': 'xxx'});

Perfect for testing your code without a REST backend during testing.

like image 190
Chev Avatar answered Oct 08 '22 22:10

Chev