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

已查看 27 次
跳至第一个未读帖子

Marcin Janowicz

未读,
2015年7月29日 10:09:452015/7/29
收件人 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

未读,
2015年7月29日 10:27:092015/7/29
收件人 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.

回复全部
回复作者
转发
0 个新帖子