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
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;
}
}
I believe this is the most popular architecture for apache nodejs angularjs.
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.
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.
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
}
});
}]);