2017-01-19 26 views
0

客戶angular2通用的後端獲取主機名

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

}) 

服務器

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

}) 

如果我的支票isBrowser沒有問題,就可以得到主機名

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

如何在後端渲染獲取主機名

+0

你想要得到什麼?本地主機連同端口號?添加您的期望值以及您當前獲得的價值。 – Aravind

+0

我得到本地主機。我想要域名(* .com) –

回答

0

如果你想用協議來獲取主機的名稱以及和其他詳細信息使用下面的代碼塔伊

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) 
}) 
1

修復server.ts

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') }` 
    }) 
}