showing results for - "how to run different node app on server different domains"
Alberto
11 Sep 2020
1var http = require('http'),
2    httpProxy = require('http-proxy');
3
4var proxy_web = new httpProxy.createProxyServer({
5        target: {
6            host: 'localhost',
7            port: 8080
8        }
9    });
10
11    var proxy_api = new httpProxy.createProxyServer({
12        target: {
13            host: 'localhost',
14            port: 8081
15        }
16    });
17
18    http.createServer(function(req, res) {
19        if (req.headers.host === 'http://www.domain.com') {
20            proxy_web.proxyRequest(req, res);
21            proxy_web.on('error', function(err, req, res) {
22                if (err) console.log(err);
23                res.writeHead(500);
24                res.end('Oops, something went very wrong...');
25            });
26        } else if (req.headers.host === 'http://api.domain.com') {
27            proxy_api.proxyRequest(req, res);
28            proxy_api.on('error', function(err, req, res) {
29                if (err) console.log(err);
30                res.writeHead(500);
31                res.end('Oops, something went very wrong...');
32            });
33        }
34    }).listen(80);