How to set baseUrl in my node.js config file
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',
}
}
I have set static baseUrl to my config file in node.js. How can I make it dynamic on different servers?
as: -
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'
I need this type of output in my config file.
+3
source to share
1 answer
As you know, module.exports returns a javascript object. therefore we can use the get / set property to change the value of any property of the object.
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);
+3
source to share