简体   繁体   中英

Node.js- Make array in app.js available in a module

In my Node app.js, I don't necessary need to have my array openConnections global...but I want it in app.js and accessible by module sse_server.js for the sseStart function. How can I do this?

app.js:

var express = require('express');
var morgan = require('morgan');
var fs = require("fs");
var createDomain = require("domain").create;
var app = express();



app.use(express.static(__dirname + '/app/static/views'));
app.use(express.static(__dirname + '/app/static'));
app.use(express.static(__dirname + '/app/images'));
app.use(express.static(__dirname + '/app'));

var openConnections = [];
var sseStart = require(__dirname + '/app/scripts/sse_server.js');

app.get('/subscribe', sseStart);

var callAllApiAndSave = require('./app/scripts/api_scripts/call_all_api.js');
var db = require(__dirname + '/app/data/db.js');

var mainDomain = new createDomain();

mainDomain.run(function () {
    mainDomain.on('error', function() {
        console.log('yoyoyoyoyoyoyoyoyoyoyoyoyoyoyo');
    });
    mainDomain.on('error', function(er) {
        console.log('error, but oh well', er.message);
    });
    db.connectDatabase();
    setInterval(callAllApiAndSave, 120000);
    var server = app.listen(9000, function() {
            console.log('Listening on port %d', server.address().port);
    });
});

sse_start.js:

function sseStart(req, res) {
    console.log(req);
    // set timeout as high as possible
    req.socket.setTimeout(Infinity);

    // send headers for event-stream connection
    // see spec for more information
    res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    });
    res.write('\n');

    // push this res object to our global variable
    console.log("Creating SSE connection for " + req.connection.remoteAddress);
    openConnections.push(res);
    console.log("Total current live connections: " +
                openConnections.length);

    // When the request is closed, e.g. the browser window
    // is closed. We search through the open connections
    // array and remove this connection.
    req.on("close", function() {
        console.log("Closing SSE connection for "
                    + req.connection.remoteAddress);
        openConnections = openConnections.filter(function(storedRes) {
            if (storedRes !== res) {
                return storedRes;
            }
        });
        if (!openConnections)  openConnections = [];

        console.log("Total current live connections: " +
                    openConnections.length);
    });
}

module.exports = sseStart;

Looking at the code you posted openConnections is only used in sse_start.js so why not put it there?

If you really want to share that array between those two files you can just put it in a separate module and require it in app.js and sse_start.js.

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