How to specify a subdomain for $resource ?

1,348 views
Skip to first unread message

Astronaute

unread,
Mar 7, 2013, 10:26:18 AM3/7/13
to ang...@googlegroups.com

Hello,

I have an Angular web application on "www.foo.com" with the server API accessible on "api" subdomain: "api.foo.com".

I have different environments (dev, staging, prod...) each deployed on their own domain (www.foo-dev.com, www.foo-staging.com, www.foo.com). Server API is always relative to that domain (api.foo-dev.com, api.foo-staging.com, api.foo.com).

I'm trying to specify the subdomain for $resource in Angular services so they can request the service API on the "api" subdomain.
Actually my services are made this way, and I cannot find a way to tell the resource to use the "api" subdomain:

myServices.factory("ProductService", ($resource) ->
  return $resource("/products/:productId", {productId:"@productId"})
)


I'm coming from Rails where we can put a subdomain constraint on any route or URL simply like this:
:constraints => { :subdomain => 'api' }

How can this be achieved using Angular $resource please?

Thank you for your help!

Andrea Reginato

unread,
Mar 8, 2013, 5:08:35 AM3/8/13
to ang...@googlegroups.com
If I got your problem correctly this should solve your problem

  myServices.factory("ProductService", ($resource) ->
    return $resource("api.foo.com/products/:productId", {productId:"@productId"})
  )

Astronaute

unread,
Mar 8, 2013, 1:06:28 PM3/8/13
to ang...@googlegroups.com
It will only work in prod (foo.com)

In order to make it work in all environments, only the subdomain should be specified, the domain should always be the one on which the angular app is running.

Rafał Filipek

unread,
Mar 8, 2013, 3:32:37 PM3/8/13
to ang...@googlegroups.com
Your ProductService can inject domain name / subdomian / or any other config via http://docs.angularjs.org/api/angular.Module
You can use "variable" or "constant". After that you can inject this variable into your ProductService.


On Thursday, March 7, 2013 4:26:18 PM UTC+1, Astronaute wrote:

Astronaute

unread,
Mar 8, 2013, 6:20:19 PM3/8/13
to ang...@googlegroups.com
I cannot just put it in the constant or variable or any other Angular object, I will have to either generate the Angular JS file on server side and inject the API URL into it or read URL in the Angular service and do some string manipulation to insert the subdomain there.
I was hoping to find a helper or something like in rails, but it shouldn't be too much work.

Thank you for your help.

Sander Elias

unread,
Mar 9, 2013, 2:50:30 AM3/9/13
to ang...@googlegroups.com
Hi, 

In angular you can use decorators. I would create an decorator on the $resource to do what you are after. 

regards
Sander Elias


On Thursday, March 7, 2013 4:26:18 PM UTC+1, Astronaute wrote:

Rahul Dabhi

unread,
Feb 4, 2018, 2:30:39 AM2/4/18
to Angular and AngularJS discussion
Hello,

I have started working on a similar application like you are developing or developed. I can not understand how to develop subdomain based applications in angularJS and nodeJS. I have developed separate restful API's in nodeJS and frontend in angularJS. Can you please guide me how to develop subdomain based applications with angularJS and nodeJS ?.
Reply all
Reply to author
Forward
0 new messages