![](/img/trans.png)
[英]How to set Nuxt.js' config for axios baseURL to work globally?
[英]how to set baseUrl in my config file node.js
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; // 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.