Ano*_*ous 7 javascript node.js
有没有办法检查nodejs连接的来源?
在我们的JavaScript中
if (window.location.host == "localhost")
{
// Do whatever
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在nodejs中做,我想做(然后我只需要为git repo维护1个文件夹)
if (window.location.host == "localhost"){
// connect to localhost mongodb
}else{
// connect to mongodb uri
}
Run Code Online (Sandbox Code Playgroud)
var os = require('os');
var database_uri;
if(os.hostname().indexOf("local") > -1)
database_uri = "mongodb://localhost/database";
else
database_uri = "mongodb://remotehost/database";
//Connect to database
mongoose.connect(database_uri,
function(err){
if(err) console.log(err);
else console.log('success');});
Run Code Online (Sandbox Code Playgroud)
查看req.headers.host
。如果是 localhost 或 127.0.0.1,请使用您的逻辑执行 localhost 操作。
http://nodejs.org/api/http.html#http_http_incomingmessage
req.headers.host
还包含端口,您可能需要在检查之前将其剪掉。
归档时间: |
|
查看次数: |
7903 次 |
最近记录: |