Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  35] [ 6]  / answers: 1 / hits: 11089  / 9 Years ago, tue, may 5, 2015, 12:00:00
var path = require('path');
module.exports = {
site: {
contactEmail: '[email protected]',
baseUrl: http://localhost:3000/,
uploadPath: path.join(__dirname, '../public'),
language:'en'
},
mongodb: {
url: 'mongodb://localhost:27017/psp',
}
}


I have set static baseUrl in my config file in node.js.How can I do dynamic in different servers?



like:-



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 want this type of output in my config file.


More From » node.js

 Answers
12

As all we know module.exports return a javascript object. so we can use get/set property for changing the value of any property of 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);

[#37421] Sunday, May 3, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aiyannam

Total Points: 642
Total Questions: 96
Total Answers: 102

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;