📜 ⬆️ ⬇️

Running multiple node.js sites on the same server

When there are several sites running at the same time on the same server, node.js requires a common launch mechanism. The simplest option, as well as the most productive one, is launching a single application that connects all the necessary sites. The connection mechanism should be unlimited for the created sites, and as simple as possible. It should be stable, with a critical error on one of the sites, others should still continue their work.
All connected web applications should be easily portable to a separate hosting. Support is needed 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 site.ru, othersite2.ru and so on.
- / var / www / subdomains / , with site3, othersite4 and so on.
')
In order for the sites to be available at the corresponding 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 the presence of directories, and in each found file includes an .index.js file, if it exists. The root .index.js file may contain routing paths for the site, usually it is from him that all server logic begins. When connected, it must export the app object, this is the un-launched http-server of the application, which is then passed to the connect.vhost library. Using connect.vhost, routing for domain names is performed.

The standard template content of the .index.js file can 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 intercepted from any site, the process will not end for everyone thanks to the universal design:

 process.on('uncaughtException', function (err) { console.log('Caught exception: ' + err); }); 

Thus, we have the ability to run a set of web applications written in node.js. Each site itself makes routing itself and is as independent as possible. In addition, there are no problems 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 connected web applications, then they should be launched as separate new programs on behalf of different users.

Source code and a small test program can be found at https://github.com/dkiyatkin/node-vhoster . Or you can install it using npm :

 $ npm install vhoster $ vhoster --help 

Any suggestions and wishes on the code are welcome.

Source: https://habr.com/ru/post/139433/


All Articles