这就是我的 app.js 的样子
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
static = require('node-static'); // for serving files
var game = require('./game.js').createGame();
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');
// This is the port for our web server.
// you will need to go to http://localhost:8080 to see it
app.listen(8080);
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
handle Events ...
});
exports.io = io;
exports.game = game;
当我尝试访问创建的 socket.io 列表器或游戏实例时,我收到错误消息说它未定义。这就是我试图在技巧中访问它的方式,js
var game = require('./app.js').game;
var socketio = require('./app.js').io;
var PlayerMove = require('./playerMove.js');
这可能是因为 trick.js 实际上是在 app.js 之前执行的(我放置了调试点并在那里得到了确认)。我该如何避免这种情况?有没有更好的方法在 node.js 中公开对象实例,我可能应该使用一些模式?