如何在我的配置文件node.js中设置baseUrl [英] how to set baseUrl in my config file node.js

查看:192
本文介绍了如何在我的配置文件node.js中设置baseUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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,如何在不同的服务器上进行动态设置?

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主机名= req.headers.host;//主机名='localhost:8080'

我希望在我的配置文件中使用这种类型的输出.

i want this type of output in my config file.

推荐答案

众所周知,module.exports返回一个javascript对象.因此我们可以使用get/set属性更改对象的任何属性的值.

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

这篇关于如何在我的配置文件node.js中设置baseUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆