简体   繁体   中英

TypeError: require(…).listen is not a function ERROR

I am trying to execute a.js file and is giving me the following error

错误

The piece of code that is apparently giving me problems is

var io = require('socket.io').listen(app);

Can someone tell what the problem is? I have very little experience with Javascript and Node.js:/

This whole thing is a bit confusing because socket.io supports all sorts of ways of initializing it and there are variations depending upon whether you want to hook up to an existing http(s) server or have it create its own server.

If you just want a socket.io server by itself, you would do this:

const io = require('socket.io')();

This uses the socket.io Server factory function and passes it default options (which assumes port 80).


If you want an http server that you can both use with Express and with socket.io, you would do this:

// create http server and start it
const app = require('express')();
const server = app.listen();

// create socket.io server and bind it to an existing http server
const io = require('socket.io')(server);

Or this:

// create Express listener instance
const app = require('express')();

// create http server and bind the Express listener instance to it
const server = http.createServer(app);

// create socket.io server and bind it to the existing http server
const io = require('socket.io')(server);

// start the http server
server.listen();

Both of these use the socket.io Server factory function and pass it an existing http server object that it will share and hook up to.

Or (this syntax not used very often), if you want to more deliberately create a stand-alone socket.io server instance yourself:

// Load socket.io Server class from socket.io module
const { Server } = require("socket.io");

// create socket.io server instance
const io = new Server();

// start socket.io server
io.listen();

Your code is not working for a couple reasons:

  1. require('socket.io') returns a Server factory function that doesn't have a .listen() method. You need a server instance to get a .listen() method.
  2. You don't pass app to a socket.io instance. You pass app to an http server instance when you create that server with http.createServer() or you hook them together automatically with app.listen() which creates the http server for you in that .listen() method.

FYI, the socket.io .listen() method on a socket.io Server instance can either accept a port number (which defaults to 80 if not passed) or it can accept an existing, already created http server instance. The Server class factory function which is what require('socket.io') returns will also accept those two types of arguments as illustrated above.


FYI, I assumed you're using Express because of your use of the app variable. If that's not the case, then please show the rest of the code that shows what app is and where it comes from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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