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) municates 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 remendation/best practices for using AngularJS without REST API backend?
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) municates 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 remendation/best practices for using AngularJS without REST API backend?
Share Improve this question edited Jun 5, 2015 at 23:08 GSaunders 5481 gold badge5 silver badges15 bronze badges asked Jun 5, 2015 at 22:17 matori82matori82 3,83910 gold badges44 silver badges67 bronze badges 3- Why do you want to use a non-RESTful backend for your application? – ThiefMaster Commented Jun 5, 2015 at 22:19
- 1 Simply use node.js for your REST API and Angular.js as client-side framework, municate with your backend. If you're afraid of REST, mind you, it's the painless job to have. – softvar Commented Jun 5, 2015 at 22:21
- @ThiefMaster, because I already have working application with node.js + jade + logic implemented in controllers and in jade files (eg. things like if/else, loops etc...). Adding the AngularJS to the mix will require that I rewrite current logic to make it available from REST API. I wanted to know if I can use AngularJS without that effort. – matori82 Commented Jun 5, 2015 at 22:36
4 Answers
Reset to default 7Absolutely. 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 munication 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.
REST which is short for Representational state transfer is basically things or resources instead of actions. Yes AngularJS can be used without REST API.
You can use nodeJS for your restful API and AngularJS as your javascript framework.
Even without a restful API AnguarlJS is a very strong tool to use in a project although to use it to it's full potential (fully scaled web app) then you would need a restful API.
use $http for not RESTful API use $resource for RESTful API