简体   繁体   English

TypeError: require(...).listen 不是 function 错误

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

I am trying to execute a.js file and is giving me the following error我正在尝试执行 a.js 文件并给我以下错误

错误

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:/我对 Javascript 和 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.这整个事情有点令人困惑,因为 socket.io 支持各种初始化它的方式,并且根据您是要连接到现有的 http(s) 服务器还是让它创建自己的服务器,会有一些变化。

If you just want a socket.io server by itself, you would do this:如果您只想要一个 socket.io 服务器本身,您可以这样做:

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

This uses the socket.io Server factory function and passes it default options (which assumes port 80).这使用 socket.io Server工厂 function 并传递默认选项(假定端口 80)。


If you want an http server that you can both use with Express and with socket.io, you would do this:如果你想要一个 http 服务器,你可以同时使用 Express 和 socket.io,你可以这样做:

// 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.这两个都使用 socket.io Server工厂 function 并将其传递给现有的 http 服务器 ZA8CFDE6331BD49EB2ACZ66B8 将共享它。

Or (this syntax not used very often), if you want to more deliberately create a stand-alone socket.io server instance yourself:或者(这种语法不经常使用),如果您想更刻意地自己创建一个独立的 socket.io 服务器实例:

// 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. require('socket.io')返回一个没有.listen()方法的Server工厂 function。 You need a server instance to get a .listen() method.您需要一个服务器实例来获取.listen()方法。
  2. You don't pass app to a socket.io instance.您不会将app传递给 socket.io 实例。 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.当您使用http.createServer()创建该服务器时,您将app传递给 http 服务器实例,或者您使用app.listen()自动将它们挂钩在一起,它会为您创建.listen()方法。

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.仅供参考,socket.io Server实例上的 socket.io .listen()方法可以接受端口号(如果未通过,则默认为 80),或者它可以接受现有的、已创建的 Z80791B3AE7002FAACB88C24688876 服务器实例。 The Server class factory function which is what require('socket.io') returns will also accept those two types of arguments as illustrated above. Server class 工厂 function 这是require('socket.io')返回的内容,也将接受这两种类型的 arguments,如上图所示。


FYI, I assumed you're using Express because of your use of the app variable.仅供参考,我假设您使用的是 Express,因为您使用了app变量。 If that's not the case, then please show the rest of the code that shows what app is and where it comes from.如果不是这种情况,那么请显示代码的 rest,以显示app是什么以及它来自哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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