0
這是我app.js外觀的Node.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中的對象實例,我應該使用一些模式?
我以爲我用'exports.game = game;'輸出變量'game'的變量值,那麼爲什麼我當我嘗試在其他文件中使用require訪問它時,將其設置爲undefined – nesta13 2013-03-20 02:42:32
只需在您的頭部運行它 – Floby 2013-03-20 09:33:56