3 different domains in node express - how to solve it ?

27 views
Skip to first unread message

Marcin Janowicz

unread,
Jul 29, 2015, 10:09:45 AM7/29/15
to Express
Hi I have issue with req.hostname - I have three different domain point to the same ip ( same node/exppress app) - and I want to send different infos based on host name - 

my code:

var express = require('express'),
app = express();

var port = process.env.PORT || 3000;

app.use(function(req,res,next){
if (req.hostname == "domain1") {
app.get('/', function(req,res){
res.send('Domain1 is active');
}) else if (req.hostname == "domain2") {
app.get('/', function(req,res){
res.send('Doamin2 is active');
});
}
next();
}
});

app.listen(port);

if I upload this to server  its works but only  first time ( if i start form first domain i've always have infos about Domain1 - it doesn't mater which host is active) and vice versa 

any advice ?


Adam Reynolds

unread,
Jul 29, 2015, 10:27:09 AM7/29/15
to expre...@googlegroups.com
You registering middleware which runs each time and registers a modal route. 

try 
app.get('/', function(req,res){
if (req.hostname == "domain1") {
res.send('Domain1 is active');
}) else if (req.hostname == "domain2") {

--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages