Running multiple node.js sites on one server
When there are several simultaneously running node.js sites on the same server, they need a common startup mechanism. The simplest option, as well as the most productive, is to launch one application that connects all the sites you need. The connection mechanism should be unlimited for the sites being created, and as simple as possible. It should be stable, with a critical error on one of the sites, others should still continue to work.
All plug-in web applications should be easily portable to separate hosting. Support is required for both individual domain names and subdomains for a specific domain name.
Suppose there are two directories in which web applications are located:
- / var / www / domains / , with the directories site.ru, othersite2.ru and so on.
- / var / www / subdomains / , with the directories site3, othersite4, etc.
In order for the sites to be accessible at the appropriate addresses (site.ru, othersite2.ru, site3.example.com, othersite4.example.com), you will need to run this command:
For those who need vhoster and its implementation is interesting, I ask under cat.
The program scans directories for directories, and in each found file includes a .index.js file, if any. The root .index.js file may contain routing of paths for the site, usually all server logic begins with it. When connected, it must export the app object, this is an unreleased http-server of the application, which is then passed to the connect.vhost library . Using connect.vhost, routing is done for domain names.
The standard template content for the .index.js file might be:
Similarly, this will work with express or connect :
If an error is not caught from any site, the process for all will not end, thanks to the universal design:
Thus, we are able to run a set of web applications written in node.js. Each site makes routing for itself and is as independent as possible. In addition, there is no problem running the site on a separate hosting without the help of vhoster, adding the following condition to the .index.js code:
Before using this technology, you need to understand its main drawback. All sites will have a common global area and the same rights. If this is unacceptable for plug-in web applications, then they must be run as separate new programs on behalf of different users.
The source code and a small test program can be found at https://github.com/dkiyatkin/node-vhoster . Or it can be installed using npm :
Any suggestions and wishes on the code are welcome.
All plug-in web applications should be easily portable to separate hosting. Support is required for both individual domain names and subdomains for a specific domain name.
Suppose there are two directories in which web applications are located:
- / var / www / domains / , with the directories site.ru, othersite2.ru and so on.
- / var / www / subdomains / , with the directories site3, othersite4, etc.
In order for the sites to be accessible at the appropriate addresses (site.ru, othersite2.ru, site3.example.com, othersite4.example.com), you will need to run this command:
$ vhoster -n example.com -s /var/www/subdomains/ -d /var/www/domains/ --port 80 --host 0.0.0.0
vhost: site.ru
vhost: othersite2.ru
vhost: site3.example.com
vhost: othersite4.example.com
For those who need vhoster and its implementation is interesting, I ask under cat.
The program scans directories for directories, and in each found file includes a .index.js file, if any. The root .index.js file may contain routing of paths for the site, usually all server logic begins with it. When connected, it must export the app object, this is an unreleased http-server of the application, which is then passed to the connect.vhost library . Using connect.vhost, routing is done for domain names.
The standard template content for the .index.js file might be:
var http = require('http');
exports.app = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
Similarly, this will work with express or connect :
var connect = require('connect');
exports.app = connect().use(function(req, res) {
res.end('hello world\n');
});
If an error is not caught from any site, the process for all will not end, thanks to the universal design:
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
Thus, we are able to run a set of web applications written in node.js. Each site makes routing for itself and is as independent as possible. In addition, there is no problem running the site on a separate hosting without the help of vhoster, adding the following condition to the .index.js code:
if (require.main === module) { // если веб-приложение запускается как отдельная программа
exports.app.listen(80); // запустить сервер
}
Before using this technology, you need to understand its main drawback. All sites will have a common global area and the same rights. If this is unacceptable for plug-in web applications, then they must be run as separate new programs on behalf of different users.
The source code and a small test program can be found at https://github.com/dkiyatkin/node-vhoster . Or it can be installed using npm :
$ npm install vhoster
$ vhoster --help
Any suggestions and wishes on the code are welcome.