简体   繁体   English

TypeError: require(...).listen 不是函数

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

I wrote this, but errors come up and i dont know how to fix我写了这个,但出现错误,我不知道如何解决

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']);

and this error comes, i don't know how to fix, tell me这个错误来了,我不知道如何解决,告诉我

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

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

require('socket.io') returns the socket.io Server class. require('socket.io')返回 socket.io 服务器类。 It's a class, not an instance and thus not something you call .listen() on.它是一个类,而不是一个实例,因此不是你调用.listen()的东西。 Depending upon what exactly you're trying to accomplish, there are a number of different ways you can use that Server class as you can see here in the doc .根据您要完成的具体任务,您可以通过多种不同的方式使用该 Server 类,如您在 doc中所见。 For example, you can do this:例如,您可以这样做:

const Server = require('socket.io');
const io = new Server(somePort);

or this:或这个:

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

or to share an existing http server:或共享现有的 http 服务器:

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

The below snippet worked for me:下面的代码片段对我有用:

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

I wrote this, but errors come up and i dont know how to fix我写了这个,但是出现错误,我不知道如何解决

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']);

and this error comes, i don't know how to fix, tell me而这个错误来了,我不知道如何解决,告诉我

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

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

Do Simply Updated Code here:在这里简单地更新代码:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(process.env.PORT || 3000);
console.log('Server is running ');

you need to add cors properly to this and set client request url in cors您需要为此正确添加 cors 并在 cors 中设置客户端请求 url

const { Server } = require("socket.io")

const cors = require("cors");

app.use(cors());


const server = http.createServer(app)

const io = new Server(server,{

    cors: {
        origin: "http://localhost:3002"  <--- here add client req url 
    }
});

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

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