请求标头undefined - nodejs SSL

时间:2016-07-20 14:43:20

标签: node.js ssl express

在启用SSL的服务器上的Express.js和Nodejs 6中使用req.headers.host时,我会收到未定义的标题错误。

我的代码:

if(req.headers.host.indexOf('domain.com')>-1){
  ......
}

错误我得到的内容:

www-0 TypeError: Cannot read property 'indexOf' of undefined

1 个答案:

答案 0 :(得分:1)

Express的文档有一节req.hostname

尝试做这样的事情:

if (req.hostname === 'domain.com') {
    ..........
}

在Express app.js文件中,您可以执行此操作,这对我有用:

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

app.get('/', function (req, res) {
    res.send('Hello World!' + ' Hostname is ' + req.hostname);
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});
相关问题