【问题标题】:Node.js http.createServer throws TypeError: listener must be a functionNode.js http.createServer 抛出 TypeError: listener must be a function
【发布时间】:2015-08-21 05:42:41
【问题描述】:

index.js:

var koa = require('koa')
  , Primus = require('primus.io')
  , http = require('http')
  , app = koa()
  , server = http.createServer(app);

var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });

primus.on('connection', function (spark) {
  spark.send('news', { hello: 'world' });
  spark.on('my other event', function (data) {
    console.log(data);
  });
});

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

server.listen(8080);
console.log('8080');

运行:node --harmony index

错误:throw TypeError('listener must be a function');

【问题讨论】:

    标签: koa engine.io primus


    【解决方案1】:

    您需要更改您的代码来执行此操作:

    server = http.createServer(app.callback())

    【讨论】:

    • 或者,使用app.listen(8080),这是http.createServer(app.callback()).listen(8080)的糖
    【解决方案2】:

    尝试:添加星号'*'

    app.get('/',function*(next){})
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2018-01-18
      • 2017-04-08
      • 1970-01-01
      • 2019-06-18
      • 2021-11-22
      • 2016-10-10
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多