angular2通用后端获取主机名

angular2通用后端获取主机名,angular,universal,Angular,Universal,客户 服务器 this._service.get('/api/user').subscribe(user => { }) 如果我的检查是浏览器没有问题,可以得到主机名 app.use('/api/user', function(req){ console.log(req.hostname) //always get localhost }) 如何在后端呈现获取主机名如果要获取主机名以及协议和其他详细信息,请使用下面的代码 if( isBrowser ) { t

客户

服务器

this._service.get('/api/user').subscribe(user => {

})
如果我的检查是浏览器没有问题,可以得到主机名

app.use('/api/user', function(req){
    console.log(req.hostname)  //always get localhost  

})

如何在后端呈现获取主机名如果要获取主机名以及协议和其他详细信息,请使用下面的代码

if( isBrowser )
{
    this._service.get('/api/user').subscribe(user => {
        this.store.dispatch({ type: SETUSER, payload: user })
    })
}
修复服务器.ts

app.use('/api/user', function(req){
    console.log(req.hostname)  //always get localhost  
    console.log(window.location.protocol + "//" + window.location.host);
    //output of the above line will be (https://stackoverflow.com)
})

你想要什么?本地主机和端口号?将您的期望值与当前获得的值相加。我获得localhost。我要获取域(*.com)
function ngApp(req, res) 
{
    res.render('index', 
    {
        req,
        res,
        preboot: false,
        baseUrl: '/',
        hostname: req.hostname,
        requestUrl: req.originalUrl,
        originUrl: `http://${req.hostname}:${ app.get('port') }`
    })
}