Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best deployment architecture for angularjs nodejs app

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)?
like image 548
lukpaw Avatar asked Nov 14 '13 17:11

lukpaw


People also ask

Which is best AngularJS or NodeJS?

Node. js is more preferable when faster and scalable web development is needed. It is usually used for building small-sized projects. Angular is preferred when real-time applications, for example, chat apps, or instant messaging are needed.

Which architecture does NodeJS follow?

Node. js uses the “Single Threaded Event Loop” architecture to handle multiple concurrent clients. Node. js Processing Model is based on the JavaScript event-based model along with the JavaScript callback mechanism.

Can I use AngularJS with NodeJS?

Both can be combined to create isomorphic web applications. NodeJS is the cross-platform and a run-time environment for Javascript applications. AngularJS is an open-source platform for web application development which is maintained by Google. In order to use NodeJS, you need to install it into your system.


1 Answers

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.

like image 132
yazaki Avatar answered Oct 21 '22 22:10

yazaki