0
test_file.js
繼主根一個文件裏面
var config = require('config');
var Ctrl = require('./Controllers');
var port = process.env.PORT || config.get("PORT") || 3000;
var cors = require('cors');
var express = require('express');
var app = express();
var router = express.Router();
app.use(cors());
app.use(router);
var server = require('http').createServer(app);
var io = require('socket.io')(server, {'pingInterval': 4000, 'pingTimeout': 6000});
io.on('connection', Ctrl.connection);
console.log("Opening at port " + port);
server.listen(port, function() {
});
module.exports = router;
require('./Routes')();
我有另外一個代碼文件路徑
/控制器/索引。 JS
我想傳遞出io
到index.js過,這裏是它的代碼
var Promise = require('q').Promise;
var config = require('config');
var mysql = require('mysql');
/// I want to get var io = require('../test_file.js');
/**Initialization socket connection */
exports.connection = function (socket) {
if (!(socket.handshake.query.accountType != null && socket.handshake.query.id != null && socket.handshake.query.accessKey != null
&& socket.handshake.query.id > 0)) {
socket.disconnect();
return;
}
現在我感到困惑module.exports和出口,我module.exports已經傳遞出對另一個文件,我想添加另一個變量,即io
並將其傳遞給控制器文件。我怎樣才能做到這一點
在響應查詢,
app.use(router);
var server = require('http').createServer(app);
var io = require('socket.io')(server, {'pingInterval': 4000, 'pingTimeout': 6000});
module.exports = {
router: router,
io: io
};
io.on('connection', Ctrl.connection);
console.log("Opening at port " + port);
server.listen(port, function() {
});
require('./Routes')();
在控制器/ index.js
var Promise = require('q').Promise;
var config = require('config');
var mysql = require('mysql');
var Driver = require('./driver');
var User = require('./user');
var io = require('../test_file.js').io;
console.log("logging");
console.log(io);
結果是{} for io
接取它,我已經做到了,但如何才能獲得VAR路由器=需要(」 ../ test_file.js')路由器。不工作 –
它應該是工作。順便說一句你得到什麼錯誤? – abdulbarik
請參閱我的編輯選項 – abdulbarik