13

I have Moto Adverts application in angularjs and nodejs. Angularjs-client-side is running on Apache HTTP Server (localhost:8000) but nodejs-server-side is runnning as node.js http server (localhost:3000).

Piece of client-side code (angularjs):

var motoAdsServices = angular.module('motoAdsServices', ['ngResource']);

motoAdsServices.factory('Brand', ['$resource', function($resource) {
    return $resource('http://localhost\\:3000/api/:id', {}, {
      query: {
        method: 'GET',
        params: {
          id: 'brands'
        },
        isArray: true
      }
    });
  }]);

Piece of server-side code (nodejs):

var express = require('express');
var path = require('path');
var http = require('http');
var brands = require('./routes/brands');

var app = express();

var allowCrossDomain = function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
};

app.configure(function() {
  app.set('port', process.env.PORT || 3000);
  app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */
  app.use(express.bodyParser()),
  app.use(allowCrossDomain);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.get('/api/brands', brands.findAll);

http.createServer(app).listen(app.get('port'), function() {
  console.log("Express server listening on port " + app.get('port'));
});

My questions are:

  1. What I should do to run client-side and server-side on the same server. a) On Apache HTTP Server (localhost:8000). b) On Node.js self http server on (localhost:3000).
  2. What architecture will be the best for production use - two independent servers for client-side and server-side or only one?
  3. Is it good practise to use Cross-origin resource sharing (CORS) on server-side (if I should hava two independed servers)?
  4. What I should do to not hard code address http://localhost:3000/api/brands to server-side (best practise)?
lukpaw
  • 1,603
  • 2
  • 26
  • 31
  • 2
    Keep in mind of cross domain issue when making ajax call. It is better to design a thin proxy later to route the ajax request to your server side. – zs2020 Nov 14 '13 at 18:20
  • 1
    I would host both (client and server code) in Node.js on port 3000and proxy them via Apache on port 80. You can then ask Apache to cache your static assets (like the client side JS and CSS ) and that way you get the best of both worlds. – Hector Correa Nov 14 '13 at 18:21
  • @sza what do you mean by thin proxy? – lukpaw Nov 14 '13 at 18:25
  • @lukpaw A light-weight server side logic put on the same host of each client side for routing the request. – zs2020 Nov 14 '13 at 18:27
  • @Hector Correa how to host both in Node.js? Extends my server-side code to be able to serve client-side? Write web-server.js to server everything? Do you know some pattern or article about it? – lukpaw Nov 14 '13 at 18:29
  • @lukpaw you can put your client-side javascript file inside the public folder (that you are already using to serve other static files) and then they will serve by Express/Node.js. – Hector Correa Nov 14 '13 at 18:31
  • @sza OK, I see and I know how to do it in java servlets :) But I don't know how to code in node.js. I understand that I should write thin proxy in node.js and host it with client-side. And my thin proxy call in backend full logic servers-side. Thanks, I try to solve it in this way. – lukpaw Nov 14 '13 at 18:38
  • @Hector Correa I don't understand it, because now I have my static content and angularjs content on Apache in public foleder. And my server-side services is written in node.js with embedded server. So what I should do: put static content and angularjs content to folder and extends my embedded server to serves this content? – lukpaw Nov 14 '13 at 18:44

3 Answers3

13
  1. Node.js
  2. One server will be more maintainable. To optimize you can cache static files with nginx later if you need to. (Just search 'nginx Node.js static' but it will work fine without that if you have light traffic and not a ton of static files). I would not use Apache for anything.

Here is a sample nginx config:

server {
  listen 80;
  server_name myserver.net;

  root /mnt/app;
  index index.html index.htm;

  location /static/ {
       try_files $uri $uri/ =404;
  }

  location /api/ {
       proxy_pass http://127.0.0.1:8080;
  }
}
  1. You won't need CORS with one.
  2. The ports will be the same so that isn't an issue.
Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
12

I believe this is the most popular architecture for apache nodejs angularjs.

enter image description here

(A) in the figure.

I recommend for you to serve all files including static files via nodejs server as I wrote in my figure. On the other hand, you could use node server only for dynamic contents and use apache to serve static files including your client side codes if you like. But if you do so, you need apache server ALWAYS even when you develop your application. I feel that will be troublesome.

(B) in the figure.

You can serve your client side codes including other static files by locating them in public directory. If you decide to serve all files from nodejs server, you can develop without apache and avoid to write specific port number in your code. I think your application code should not be conscious about the port number you will use.

I hope this could be answer for your all questions.

yazaki
  • 1,724
  • 1
  • 13
  • 17
6

What I should do to run client-side and server-side on the same server. a) On Apache HTTP Server (localhost:8000). b) On Node.js self http server on (localhost:3000).

Ans : you don't need to run nodejs as self hosted.instead run nodejs through Apache server and use fusion passenger too. fusion passenger will take care of running your node application in background forever. Personally I prefer Nginx + fusion for my nodejs applications.

What architecture will be the best for production use - two independent servers for client-side and server-side or only one?

Ans : I don't understand what you mean by having two servers one for client and one for server-side. Keep your client and server code on single server.

Is it good practice to use Cross-origin resource sharing (CORS) on server-side (if I should have two independent servers)?

Ans : if your server and your client are under same domain then you don't need to worry about CORS but if in future you want to expose your API to any of your client apps, then you will need to do CORS configurations.

What I should do to not hard code address http://localhost:3000/api/brands to server-side (best practice)?

Ans : I use constant to declare my base path and then do the DI in my services and factory that make the API Calls.

motoAdsServices.constant("API", {
        "endpoint": "http://localhost:8080",
    })

motoAdsServices.factory('Brand', ['$resource','API', function($resource,API) {
    return $resource(API.endpoint + '/api/:id', {}, {
      query: {
        method: 'GET',
        params: {
          id: 'brands'
        },
        isArray: true
      }
    });
  }]);
Grimxn
  • 22,115
  • 10
  • 72
  • 85
maddygoround
  • 2,145
  • 2
  • 20
  • 32