如何从routes / index访问io.emit()? 上快递4.15

斌/ www.js

var server = http.createServer(app); io = require('socket.io')(server); io.on('connection', function(client) { console.log('Client connected...'); client.on('join', function(data) { console.log(data); }); }); server.listen(port); 

在这里我需要通过socket.io访问或发送消息或mongoose结果(我不知道这是可能的或不?

我需要从routes/index.js页面发送消息给客户端。 我试过的是

 module.exports.io = io; //(in `bin/www`) 

并访问index.js这样的io像这样var io = require('../ bin / www');

 io.on('connection', function(client) { console.log('Client connected...'); client.on('join', function(data) { console.log(data); }); }); 

但它会有一些错误

 io.on('connection', function(client) { ^ TypeError: io.on is not a function.... 

我的需要是, 我想发送dynamic的结果给客户端当路由被调用或发送一些mongoose查询结果给客户端使用socket.io是可能的吗?

在express 4.15上var server = http.createServer(app); 在bin / www.js上

app.js

 var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var index = require('./routes/index'); var users = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); /*app.use(function(req, res, next) { req.headers['if-none-match'] = 'no-match-for-this'; next(); });*/ //app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public'), { maxAge: '30d', setHeaders: function (res, path) { res.setHeader('Cache-Control', 'public, max-age=14770') } })); console.log("PATH"+path.join(__dirname, 'public')); app.io = require('socket.io')(); app.io.on('connection', function(socket){ // do whatever you want }); const userRoute = require('./routes/index')(app.io); app.use('/', userRoute); app.use('/users', users); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handler app.use(function(err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app; 

更新Ashish给出的代码后出现错误

 PATHC:\Users\ADSS\Desktop\PrintAdmin\public 

C:\ Users \ ADSS \ Desktop \ PrintAdmin \ node_modules \ express \ lib \ router \ index.js:635返回fn.apply(this,arguments); ^

 TypeError: Cannot read property 'apply' of undefined at 

C:\ Users \ ADSS \ Desktop \ PrintAdmin \ node_modules \ express \ lib \ router \ index.js:635:14(C:\ Users \ ADSS \ Desktop \ PrintAdmin \ node_modules \ express \ lib \ router \ index。在路由器(C:\ Users \ ADSS \ Desktop \)下的Function.handle(C:\ Users \ ADSS \ Desktop \ PrintAdmin \ node_modules \ express \ lib \ router \ index.js:174:3) PrintAdmin \ node_modules \ express \ lib \ router \ index.js:47:12)。 (Module.js:579:10)在Module._compile(module.js:570:32)上的位置(C:\ Users \ ADSS \ Desktop \ PrintAdmin \ app.js:46:44) )在Module.load(module.js:487:32)上,在Module.require(module.js:497)的Function.Module._load(module.js:438:3),在tryModuleLoad(module.js:446:12) :17)在要求(内部/ module.js:20:19)在对象。 (module.js:579:10)在Module._compile(module.js:570:32)上的位置(C:\ Users \ ADSS \ Desktop \ PrintAdmin \ bin \ www: )在Module.load(module.js:487:32)

       

网上收集的解决方案 "如何从routes / index访问io.emit()? 上快递4.15"

我想你已经使用生成app.js和bin / www文件的快递生成器生成了应用程序,在路由内部使用io对象存在循环依赖问题。 这是你可以做的,

在app.js里面

 app.io = require('socket.io')(); // initialize io, attach server in www // use socket events here app.io.on('connection', function(socket){ // do whatever you want }) // you can pass app.io inside the router and emit messages inside route. const userRoute = require('./routes/user')(app.io); app.use('/users', userRoute); 

里面bin / www

 var io = app.io; var server = http.createServer(app); io.attach(server); 

下面是路由器的样子,user.js

 module.exports = function(io){ router.get('/', function(req, res, next){ io.emit("message", "your message"); res.send(); }) // edited return router; } 

注意:我还没有尝试过。

但是,你可以尝试这种模式呢?

在www.js

  module.exports = function(io) { io.on('connection', function(client) { console.log('Client connected...'); client.on('join', function(data) { console.log(data); }); }); } 

在index.js中

 var server = http.createServer(app); io = require('socket.io')(server); require('./wwww.js)(io);