Socket.IO error 'listen()' method expects an 'http.Server' instance after moving to Express 3.0

前端 未结 2 709
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 18:53

I began learning Node.js today and I\'m a little stuck.

Following this example, I get the following error when I try executing the js file:

Warning: expr         


        
相关标签:
2条回答
  • 2021-02-12 19:28

    There was a change to how you initialize express apps between versions 2 and 3. This example is based on version 2 but it looks like you've installed version 3. You just need to change a couple of lines to set up socket.io correctly. Change these lines:

    var app = require('express').createServer(),
        io = require('socket.io').listen(app),
        scores = {};                                
    
    // listen for new web clients:
    app.listen(8080);
    

    to this:

    var express = require('express'),
        app = express()
      , http = require('http')
      , server = http.createServer(app)
      , io = require('socket.io').listen(server);
    
    // listen for new web clients:
    server.listen(8080);
    
    0 讨论(0)
  • 2021-02-12 19:28

    Simplified to three lines of code that would be:

    var server = require('http').createServer(require('express')()),
    io = require('socket.io').listen(server);
    server.listen(8080);
    
    0 讨论(0)
提交回复
热议问题