简体   繁体   English

导出引用并导出指针node.js

[英]Exports reference and exports pointer node.js

I am having problem with exporting my routing table in my express application. 我在快速应用程序中导出路由表时遇到问题。

First in /bin/www.js file I got my global variable which reporesents routing table: 首先在/bin/www.js文件中,我得到了重新分配路由表的全局变量:

var routingTable = [];

Then to create a simple routing I am just pushing new routing objects using server listening event. 然后,要创建一个简单的路由,我只是使用服务器侦听事件推送新的路由对象。

server.on('listening', onListening);

function onListening() {
     var addr = server.address();
     var bind = typeof addr === 'string'
     ? 'pipe ' + addr
     : 'port ' + addr.port;
  console.log('Listening on ' + bind);
  routingTable.push({
     address: "localhost:8080",
     version: 1,
     name: "some-service"
  });
}

When i console.log routingTable after push i got this single object in it. 当我推后console.log routingTable我有这个单一的对象。

Then I created a getter for that variable: 然后,我为该变量创建了一个吸气剂:

exports.getRouting = function() {
   return routingTable;
};

This getter is exported to routes file: 此获取器将导出到路由文件:

var getRoutingTable = require('../bin/www').getRouting;

After I use this getRoutingTable function i got an empty array. 使用此getRoutingTable函数后,我得到了一个空数组。 What is the right way to export this variable so it can be use and updated in every other project file? 什么是导出此变量的正确方法,以便可以在其他每个项目文件中使用和更新此变量?

Is routingTable define in '../bin/www'? 路由表是否在“ ../bin/www”中定义?

var routingTable = [];
...
exports.getRouting = function() {
    return routingTable;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM