简体   繁体   English

Express.io与Express Router返回错误

[英]express.io with express router returns error

When I try to emit the 'ready' event from client on express.io, it shows an error that 当我尝试在express.io上从客户端发出“ ready”事件时,它显示错误消息

{ route: [Function], broadcast: [Function] } 'req.io.route' TypeError: Object # has no method 'ready {route:[Function],广播:[Function]}'req.io.route'TypeError:对象#没有方法'ready

I am using express version express@4.12.4. 我正在使用Express版本Express@4.12.4。

app.js is as shown below app.js如下图所示

    var routes = require('./routes/index');
var users = require('./routes/users');
var app = require('express.io')();
app.http().io();
//var app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

and the index.js file is as below 并且index.js文件如下

app = require('express.io')()
app.http().io()
var router = app.Router();

/* GET home page. */
router.all('/', function(req, res, next) {
  res.render('index', { title: 'Express' });


    req.io.route('ready', function(res) {
         console.log('tested..........');
    })


});

module.exports = router;

client side code is as below 客户端代码如下

<script>

  var socket;
 $(document).ready(function(){

 socket=io.connect(window.location.hostname);
    socket.emit('ready'); 

});

  </script>
</html>

please help me regarding this... 请帮助我...

The router isn't a method on the return value of the constructor, it's exported with the module. 路由器不是构造函数返回值的方法,而是与模块一起导出的。

var express = require('express.io');
var app = express();
app.http().io()
var router = express.Router();

Also app.http().io() only needs to be called once, so remove it from your routes file. 而且app.http().io()仅需要被调用一次,因此将其从路由文件中删除。

Try 尝试

/* GET home page. */
router.all('/', function(req, res, next) {
    app.io.route('ready', function(res) { //not req.io... 
         console.log('tested..........');
    })
    res.render('index', { title: 'Express' });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM