Node.js http.createServer抛出TypeError:listener必须是一个函数

时间:2014-09-23 09:19:25

标签: koa engine.io primus

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');

2 个答案:

答案 0 :(得分:3)

您需要更改代码才能执行此操作:

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

答案 1 :(得分:1)

尝试:添加星'*'

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