Javascript TypeError:需要(…)。侦听不是函数

Javascript TypeError:需要(…)。侦听不是函数,javascript,node.js,server,socket.io,Javascript,Node.js,Server,Socket.io,我写了这个,但是出现了错误,我不知道如何修复 var http = require('http'); var clientHtml = require('fs').readFileSync('client.html'); var plainHttpServer = http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/html' }); response.

我写了这个,但是出现了错误,我不知道如何修复

var http = require('http');
var clientHtml = require('fs').readFileSync('client.html');

var plainHttpServer = http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(clientHtml);
}).listen(8080);

var files = require('fs');

var io = require('socket.io').listen(plainHttpServer);
io.set('origins', ['localhost:8080', '127.0.0.1:8080']);
这个错误来了,我不知道如何修复,告诉我

var io = require('socket.io').listen(plainHttpServer);
                          ^

TypeError: require(...).listen is not a function
require('socket.io')
返回socket.io服务器类。它是一个类,不是一个实例,因此也不是您调用的对象。正如您所看到的,有许多不同的方法可以使用该服务器类,具体取决于您要完成的任务。例如,您可以执行以下操作:

const Server = require('socket.io');
const io = new Server(somePort);
或者这个:

const io = require('socket.io')(somePort);
或共享现有http服务器:

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

下面的代码片段对我很有用:

const io = require('socket.io')(server);
...
server.listen(3000)

是的,@Abra是正确的,这看起来像javascript。但是,错误是require()不返回可以使用函数listen()的内容。是否安装了节点包socket.io?在您的命令行中,在包含该节点脚本的目录中,执行
npm install socket.io
。只需添加到该命令行中,最新版本之前的版本都支持listen()函数