简体   繁体   中英

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:

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.

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. 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'?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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