A
A
Alexey Zuykov2016-02-24 08:15:00
Domain name market
Alexey Zuykov, 2016-02-24 08:15:00

How to do domain routing in nodejs on VDS?

Task:
There are several different domains that are delegated to one VDS IP on which the node js application hangs. Is it possible to do (and how, where to dig) for the node to determine by the soul of which domain the appeal came and give the corresponding content? Like how virtual hosts in nginx are directed to the correct directory. Just do not need to send to the directory, just generate your content for each domain.
For example, so that domains are routed first, and only then by urls (such as rest).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2016-02-24
@alex_sawyer

the requested host is passed in the Host request header,
all web servers rely on this header.
Here is a simple example with express:

var http = require('http');
var express = require('express');
var site1App = express();
var site2App = express();

var site1Req = site1App();
var site2Req = site2App();
var server = http.createServer((req, res) => {
    switch(req.headers.host) {
        case 'site1.ru':
        case 'www.site1.ru':
            site1Req(req, res);
            break;
        case 'site2.ru':
        case 'www.site2.ru':
            site2Req(req, res);
            break;
        default:
            //unknown host
            res.writeHead(404);
            res.end();
    }
});
server.listen(80);

I
Ivan, 2016-04-06
@LiguidCool

Another good solution would be to put the same nginx as a reverse-proxy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question