Reputation: 2959
I'm using mainly node.js for my projects and I use nodemon
for development and forever
for production.
I usually split my projects in vhosts so my structure could be something like this:
bootstrap.js
apps/
admin/
front/
api/
and my bootstrap.js look like this:
// Get Express
var express = require('express');
// Create express server
var app = express.createServer();
// Configure Development Environment
app.configure('development', function() {
//app.use(express.vhost('localhost', require('./apps/front')));
app.use(express.vhost('localhost', require('./apps/admin')));
app.use(express.vhost('localhost', require('./apps/api')));
app.listen(3000);
});
// Configure Production Environment
app.configure('production', function() {
app.use(express.vhost('example.com', require('./apps/front')));
app.use(express.vhost('admin.example.com', require('./apps/admin')));
app.use(express.vhost('api.example.com', require('./apps/api')));
app.listen(80);
});
When I want to start my apps I use forever start bootstrap.js
and bootstrap.js
starts my defined vhost apps.
In that way I have each vhost listening in it's own port and vhost listening on 3000.
My problem now is that forever monitor's bootstrap.js and this have as a result that if any of vhost fails for any reason forever will reboot bootstra.js which will result in unnecessary restart of other vhosts too.
My question is what would be the best way to go in my case?
I though of instead having one bootstrap.js file to have a structure like this:
admin.js
front.js
api.js
apps/
admin/
front/
api/
and use forever start
for each one of those root js files, in this case I won't be using vhost but I believe vhosts are something that in one way or another I need them.
I though that in case I drop connect's vhosts to use nginx and have the following configuration for each app:
server
{
listen 80;
server_name admin.example.com;
access_log /var/log/nginx/admin.example.com.access.log;
error_log /var/log/nginx/admin.example.com.error.log;
root /home/panosru/domains/example.com/apps/admin/public;
location ~* ^.+\.(svg|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
root /home/panosru/domains/example.com/apps/admin/public;
}
location / {
proxy_pass http://admin.example.com:3001;
}
}
I can see that with nginx I have some more benefits like I use nginx to serve my static content's instead of node, which would be an unnecessary overhead for node.
What do you think? Am I correct or you could propose me something better instead?
Thanks!
Upvotes: 3
Views: 2142
Reputation: 3605
I think you basically answered your own question with a reasonable answer, but you're looking for some alternatives...
I'm not familiar with the vhost functionality in express. However, I think you're asking for trouble by running what is effectively 3 different apps with 3 different scalability needs in a single Node.js process.
You could still use the express vhosts solution by using child_process.fork to launch multiple instances of express (all of which could still share the same http server).
Upvotes: 1