Ivan Bravo Carlos
Ivan Bravo Carlos

Reputation: 1740

Node.js on multiple domains using express.vhosts()

I'm back here with a nodejs problem, I am writing a node server that allows two or more app.js running on the same system using express.vhost(). But I'm quite lost now.

The webhost server is a dedicated server running Ubuntu and plesk and I've assigned 2 ip's for different domains.

xxx.xxx.xxx.123 IP is assigned to domain-one.com xxx.xxx.xxx.xxx.456 is assigned to domain-two.com

both run a nodejs server app.js and are each allocated in /var/www/vhosts/[domain-name]/node/app.js

The server running the vhost is at /var/www/node/server.js here is the code

var express = require("express");
var app = express();

app
.use(express.vhost('domain-one.com', require('/var/www/vhosts/domain-one.com/node/app.js').app))
.use(express.vhost('domain-two.com', require('/var/www/vhosts/domain-two.com/node/app.js').app))
.listen(3030);


app.get('/', function(req, res){
  res.send('hello world the server running ');
});

While in each app.js

var express = require('express'),
    routes  = require('./routes');

var app = exports.app = express.createServer();

app.get('/', function(req, res){
  res.send('hello world test file for [domain-name] running');
});

//port 3031 for domain-one.com
//port 3032 for domain-two.com
app.listen(3031); 

then i run node server.js and every thing works fine without errors.

then i run a netstat -anltp

tcp        0      0 0.0.0.0:3030            0.0.0.0:*               LISTEN      19839/node      
tcp        0      0 0.0.0.0:3031            0.0.0.0:*               LISTEN      19839/node      
tcp        0      0 0.0.0.0:3032            0.0.0.0:*               LISTEN      19839/node  

Ok everything goes as i expected (i suppose) so i open my browser and type domain-one.com:3031 and in other tab domain-two.com:3032

but drops a Connection time-out in both domains... and when i run domain-one.com:3030 it displays the:

hello world the server running

But not in domain-two.com:3030 it hangs also..

I, want to get my head around this and understand a bit about how my server and domains work and how to manage to run diferent nodejs apps for diferent ip/domains in my server...

somethimes the domain-two.com prints what the domain-one.com app.js file res.send() supposed to print on the other domain...

I guess im very confused now... hope you can help me out with this..

Thanks a lot

-ivan

Upvotes: 3

Views: 5266

Answers (1)

migmaker
migmaker

Reputation: 2516

Perhaps better with this simple and precise syntax:
https://github.com/expressjs/vhost

//
// Module dependencies
//
var express = require('express');
var vhost = require('vhost');
var app = express();


//
// vhosts
//
app
  .use(vhost('app1.io', require('./app1/app.js')))
  .use(vhost('app2.io', require('./app2/app.js')))
  .use(vhost('app3.io', require('./app3/app.js')))
  .listen(8080);

Upvotes: 4

Related Questions