5
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 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.

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
Mustafa bw
  • 1,290
  • 3
  • 12
  • 21

1 Answers1

4

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);
Bharat Bhushan
  • 2,077
  • 2
  • 21
  • 34
  • just call this setBaseUrl function where you get hostName from request. ` http.createServer(function (req, res) { var hostname = req.headers.host; // hostname = 'localhost:8080' //Here config is your module.exports object config.setBaseUrl(hostname); var pathname = url.parse(req.url).pathname; // pathname = '/MyApp' console.log('http://' + hostname + pathname); res.writeHead(200); res.end(); }).listen(8080);` – Bharat Bhushan May 05 '15 at 08:36
  • can i add protocol with hostname...this code is working for me only i have to add dynamic protocol (http or https). – Mustafa bw May 05 '15 at 11:41
  • Its your call to add protocol with hostname or not. I just suggest you the way how you can set dynamically baseUrl. If this is working for you than don't forgot to accept (mark) as an answer. – Bharat Bhushan May 05 '15 at 11:46
  • let me know what kind of help you need? – Bharat Bhushan May 05 '15 at 11:52
  • i have to add dynamic protocol (http or https).with req.headers.host – Mustafa bw May 05 '15 at 11:53
  • Dear add new property for protocol as same like for baseUrl. Could you please create a plunker for the case. – Bharat Bhushan May 05 '15 at 11:56
  • dont have an idea about plunker – Mustafa bw May 05 '15 at 11:58
  • Its seems you don't want to do even try. Refer this line that solved your problem. http://stackoverflow.com/questions/10348906/how-to-know-if-a-request-is-http-or-https-in-node-js – Bharat Bhushan May 05 '15 at 12:01
  • can u give me ur skype id – Mustafa bw May 05 '15 at 12:03
  • yes sure but I will be available after 9:30 PM IST. skype-id bharat-bhushan5 – Bharat Bhushan May 05 '15 at 12:04