gpt4 book ai didi

javascript - 如何在我的配置文件 node.js 中设置 baseUrl

转载 作者:搜寻专家 更新时间:2023-10-31 22:48:08 25 4
gpt4 key购买 nike

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',
}
}

我在 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);

var hostname = req.headers.host;//主机名 = '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);

关于javascript - 如何在我的配置文件 node.js 中设置 baseUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com