Mus*_* bw 5 javascript node.js express
var path = require('path');
module.exports = {
site: {
contactEmail: 'info@ankhor.org',
baseUrl: "http://localhost:3000/",
uploadPath: path.join(__dirname, '../public'),
language:'en'
},
mongodb: {
url: 'mongodb://localhost:27017/psp',
}
}
Run Code Online (Sandbox Code Playgroud)
我在node.js的配置文件中设置了静态baseUrl.我可以在不同的服务器上做动态吗?
喜欢:-
var http = require('http');
var url = require('url') ;
http.createServer(function (req, res) {
var hostname = req.headers.host; // hostname = 'localhost:8080'
var pathname = url.parse(req.url).pathname; // pathname = '/MyApp'
console.log('http://' + hostname + pathname);
res.writeHead(200);
res.end();
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)
var hostname = req.headers.host; // hostname ='localhost:8080'
我想在配置文件中输出这种类型的输出.
众所周知, module.exports 返回一个 javascript 对象。所以我们可以使用 get/set 属性来更改对象的任何属性的值。
module.exports={
baseUrl : "/xyz",
setBaseUrl : function(url){
this.baseUrl = url;
}
getBaseUrl : function(){
return this.baseUrl;
}
}
var http = require('http');
var url = require('url') ;
var config = require('path/to/your/configFile');
http.createServer(function (req, res) {
var hostname = req.headers.host; // hostname = 'localhost:8080'
config.setBaseUrl(hostname);
var pathname = url.parse(req.url).pathname; // pathname = '/MyApp'
console.log('http://' + congif.getBaseUrl() + pathname);
res.writeHead(200);
res.end();
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4241 次 |
| 最近记录: |