The best deployment architecture for angularjs nodejs app

后端 未结 3 1877
無奈伤痛
無奈伤痛 2021-02-07 15:56

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 ht

相关标签:
3条回答
  • 2021-02-07 16:45
    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.
    0 讨论(0)
  • 2021-02-07 16:45

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

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

    0 讨论(0)
  • 2021-02-07 16:52

    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
          }
        });
      }]);
    
    0 讨论(0)
提交回复
热议问题