A
A
Alexander Ivanov2019-07-20 12:29:42
Node.js
Alexander Ivanov, 2019-07-20 12:29:42

Why does koa + socket.io throw an error - TypeError: Class constructor Application cannot be invoked without 'new'?

const
    http = require('http'),
    koa = require('koa'),
    app = koa(),
    serve = require('koa-static'),
    server = http.createServer(app.callback()),
    io = require('socket.io')(server);


app.use(serve('./public'));

io.on('connection', function (socket) {
  socket.on('click', function (data) {
    //process the data here
    console.log('client clicked! data:');
    console.log(data);

    // emit an event
    console.log('responding with news');
    socket.emit('news', { hello: 'world' });
  });
});

var port = process.env.PORT || 3000;
server.listen(port);
console.log ('Listening at port ' + port + ' ...');

error TypeError: Class constructor Application cannot be invoked without 'new'
More precisely, how to get around this, why is clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2019-07-20
@cimonlebedev

just Koa had to be registered in a constant, sorry

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question