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?

3
  • 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). Commented May 28, 2013 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. Commented May 28, 2013 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. Commented May 28, 2013 at 19:47

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, I didn't know about express but it seems to do what I was looking for. Thanks
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;
    }
}

3 Comments

I don't want to use NGinx. I want only one Node.JS server.
I don't really care about performances. The application is a wiki. People installing the wiki will need Node.JS. I don't want to make them install Nginx and configure it too.
@StanislavLomadurov this results in slow performance of app. the load time has decreased by twice
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.

3 Comments

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.
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.