2016-09-27 78 views

回答

2

快递4.x版自带req.subdomains,但如果你使用的是旧版本还是想发挥自己的代码,然后可以使用其他框架,以及那么你可能会喜欢

var app = express(); 

app.use(function(req, res, next) { 
    var host = req.get('host'); 
    console.log(getSubdomain(host)); 
    console.log(getSubdomainList(host)); 
    next(); 
}) 

function getSubdomain(host) { 
    var subdomain = host ? host.substring(0, host.lastIndexOf('.')) : null; 
    return subdomain; 
} 

function getSubdomainList(host) { 
    var subdomainList = host ? host.split('.') : null; 
    if(subdomainList) 
     subdomainList.splice(-1, 1); 
    return subdomainList; 
}