对于angularjs应用的NodeJS最好的部署架构 [英] The best deployment architecture for angularjs nodejs app

查看:189
本文介绍了对于angularjs应用的NodeJS最好的部署架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在angularjs和摩托的NodeJS应用广告。
Angularjs - 客户端是Apache HTTP服务器(本地主机:8000)上运行,但的NodeJS服务器端被作为捉迷藏Node.js的http服务器(本地主机:3000)。

客户端code海贼王(angularjs):

  VAR motoAdsServices = angular.module('motoAdsServices',['ngResource']);motoAdsServices.factory('品牌',['$资源',函数($资源){
    返回$资源(的http:// \\\\本地主机:3000 / API /:身份证',{},{
      查询:{
        方法:GET,
        params:一个{
          ID:'品牌'
        },
        IsArray的:真
      }
    });
  }]);

服务器端code的一块(的NodeJS):

  VAR前preSS =要求('前preSS');
VAR路径=要求('路径');
VAR HTTP =要求('HTTP');
VAR品牌=要求('./路线/品牌');VAR应用=前preSS();VAR allowCrossDomain =功能(REQ,资源,下一个){
  res.header(访问控制允许原产地,*);
  res.header(访问控制允许报头,X-要求,随着);
  下一个();
};app.configure(函数(){
  app.set('口',process.env.PORT || 3000);
  app.use(如press.logger('开发')); / *'默认','短','微小','开发'* /
  app.use(如press.bodyParser()),
  app.use(allowCrossDomain);
  app.use(如press.static(path.join(__目录名称,'公共')));
});app.get('/ API /品牌',brands.findAll);http.createServer(APP)。听(app.get('端口'),功能(){
  的console.log(前preSS服务器侦听端口+ app.get('端口'));
});

我的问题是:


  1. 我应该做些什么来在同一台服务器上运行的客户端和服务器端。
    a)在Apache HTTP服务器(本地主机:8000)。
    b)于上(本地主机的Node.js自我http服务器:3000)

  2. 什么架构将是最好的用于生产用途 - 用于客户端和服务器端或只有一个
  3. 两个独立的服务器
  4. 它是很好的做法,在服务器端使用跨源资源共享(CORS)(我是否应该哈瓦2 independed服务器)?

  5. 我应该怎么做才能不硬code类地址的的http://本地主机:3000 / API /品牌的服务器端(最佳实践)


解决方案

  1. 的Node.js

  2. 一台服务器会更容易维护。为了优化您可以缓存静态文件nginx的以后,如果你需要。 (只要搜索nginx的Node.js加载静态的,但它会正常工作没有,如果你有交通灯,而不是大量的静态文件)。我不会用Apache的任何东西。

下面是一个简单的nginx的配置:

 服务器{
  听80;
  SERVER_NAME myserver.net;  根到/ mnt /应用;
  指数的index.html index.htm的;  位置/静态/ {
       try_files $ URI $ URI / = 404;
  }  位置/ API / {
       proxy_pass http://127.0.0.1:8080;
  }
}

<醇开始=3>

  • 您将不再需要CORS一个。

  • 的端口将是相同的,所以这不是一个问题。

  • 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)?

    解决方案

    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.

    这篇关于对于angularjs应用的NodeJS最好的部署架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆