9

I am writing an application with Angular.js and Node.js.

There is a client-side app written in HTML & Angular.js that needs a web server like Apache to be online.

There is also a server-side REST webservice written in Node.js, built on restify (but I don't care which REST API I use, I can use another one).

I can get the whole thing working using a Node.js server for the REST webservice, and another Node.js server for serving the client-side webapp. But I'd like to have only one Node.js server running, on one URL/port (to prevent cross-domain AJAX requests).

How can I do so?

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • You can just have a top-level qualifier in your URLs to route requests to one application or the other. Or else use a virtual host name to separate the two (which may complicate things if the client app needs to get to the REST services, I guess). – Pointy May 28 '13 at 19:21
  • @Pointy Yes but it seems like tricks to me. I'd rather find a good solution for this (one server only). I'm used to PHP and other server-side languages, and with them you have the webserver providing the static files *and* executing the server-side scripts. So I'm trying to have the same with JS. – Matthieu Napoli May 28 '13 at 19:25
  • Well one way or another the single server will have to examine incoming HTTP requests to decide which application should handle it. To do that, it can look at the host name, the port number, or the request path. – Pointy May 28 '13 at 19:47

3 Answers3

8

Not sure if this is applicable to your current problem - but app.use() in Express can let one main application set sub-applications to handle different route prefixes. So you could have your main application point any requests starting with /store/ to one Express app, and any requests to /app/ with a second Express app.

http://expressjs.com/api.html#app.use

Plato
  • 10,812
  • 2
  • 41
  • 61
5

You can use a proxy before Nodejs. Fastest nginx

Example (nginx):

server {
    listen 80;
    server_name example.com

    # Only http://example.com/api/~
    location /api/ {
        proxy_pass  http://localhost:8000; # node.js app
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    location ~* \.(jpg|jpeg|gif|png|css|js|json|woff|zip|tgz|gz|swf|ico|txt|xml)$ {
        expires max;
        root /var/www/site_path;
    }
}
Community
  • 1
  • 1
2

You want to serve both client side app and API from same URL...

(to prevent cross-domain AJAX requests).

Why? This doesn't scale and goes against standard restful API implimentations. Ultimately you are going to want to support CORS because universal adpation will be here as early as next year with the new IE11 and IE12 rollouts. JSONP can be a fallback until they arive.

There is nothing wrong with cross-domain AJAX requests and are recently encouraged --- hence the widespread adoption of this convention.

And if you really need to restrict cross domain API requests, just whitelist the domains you want to grant access too under your node server --- easy as that.

You want to serve both client side app and API from same port...

  1. Proxy pass node server on api.domain.com through NGINX.

  2. Move client-side app to static doc root under NGINX.

Now both are sitting on PORT 80, and only one node server is being used.

Dan Kanze
  • 18,485
  • 28
  • 81
  • 134
  • I want something simple to setup. The application is a wiki. I want people to install Nodejs, fire one server through Node and be done with it. No Nginx. And I've always used one endpoint in all my other developments (PHP, ruby, ...). I don't see why I should bother now to have 2 servers for something so simple as a small webapp... I don't need scalability. – Matthieu Napoli May 28 '13 at 20:41
  • @MatthieuNapoli NGINX proxy pass is an extremely simple, common setup, also common practise. It litterly takes 5 minutes. Not to mention now you can throw your client side app up on a CDN with CORS enabled so you dont even need to host it on your own box if you dont want too. – Dan Kanze May 28 '13 at 23:19
  • I see that it's not a complex configuration, but understand that for a end-user, having to install NodeJS + Nginx + configure Nginux just to have a wiki rolling, it's not very attractive. – Matthieu Napoli May 29 '13 at 09:16