socket.io:io.origins不是一个函数



我正试图为我的socket.io nodejs服务器启用跨源同域请求,但出于某种原因,它一直告诉我io.origins不是一个函数

var io = require('socket.io', 3000);
io.origins("*");

TypeError:io.origins不是函数

**更新,跨来源错误:**

阻止跨来源请求:同源策略不允许读取http://:3000/socket.io/?上的远程资源?EIO=3&transport=轮询&t=NMWwT1R。(原因:CORS请求未成功(。

编辑2:至于为什么会出现CORS错误,是因为您没有正确配置Socket.IOcors选项。

现在,Socket.IO的cors配置实际上被传递到NPM包cors,因此您需要使cors配置与cors包一起工作。

对于您的情况,这看起来像:

cors: {
// The `*` is used as the wildcard here.
origin: "*",
// Set the other options according to your needs.
// The docs are here:
// https://www.npmjs.com/package/cors#configuration-options
methods: ["GET", "POST"],
allowedHeaders: ["content-type"]
}

编辑1:现在我知道您正在使用Socket.IO 3.0,我可能会更好地帮助您。

Socket.IO 3.0包含了一系列突破性的更改(请参阅此处的完整更改日志(,这可能就是您获得TypeError: io.origins is not a function的原因。

对于CORS错误,Socket.IO 3.0删除了一些旧选项,并添加了一些用于CORS管理的新选项。删除的选项之一是origins属性。这就是为什么您的代码不起作用:您实际上没有设置任何CORS配置。要在Socket.IO 3.0中设置CORS配置,请使用以下选项:

// I still use a port here, because it works, but you could use @Aryas'
// code if you wish.
const io = socketIO(3000, {
// Now, the CORS config.
// You could either use the new `cors` property...
cors: {
origin: "https://example.com",
methods: ["GET", "POST"],
allowedHeaders: ["content-type"]
},
// ...Or the old `allowRequest` function.
allowRequest: function(req, callback) {
callback(null, req.headers.referer.startsWith("https://example.com"));
}
});

哦,而且,在Express中设置CORS选项是不必要的;Socket.IO会在调用任何Express处理程序之前自动拾取对其的请求。

原始答案
它告诉您这一点,因为您的io变量不是Socket.IO服务器的实例。相反,它是Socket.IO服务器构造函数。为什么?

好吧,你有这个代码:

// You are passing two parameters to `require`: "socket.io", and 3000
var io = require("socket.io", 3000);

require返回模块导出的任何内容,在本例中,模块是Socket.IO服务器构造函数(而不是它的实例(。传递给require3000不会执行任何操作,因为require只接受一个参数。

为了获得Socket.IO服务器实例,您需要执行以下操作:

// The clearer way:
// `socketIO` is the Socket.IO server constructor.
var socketIO = require("socket.io");
// `io` is the Socket.IO server instance.
var io = socketIO(3000);
// The shorter way:
// Calling the result of `require` (the Socket.IO server constructor) as a function.
var io = require("socket.io")(3000);

更新2

正如你在这篇文章中提到的https://pastebin.com/1TDhWxaC代码我发现你正在使用firefox在Chrome或其他浏览器上尝试这个代码,我认为它会在浏览器上工作

firefox中存在错误或问题证书或其他内容,您会在以下所有情况下注意到

我建议你仔细阅读以下问题和答案

  1. Firefox"跨源请求被阻止",尽管有标题
  2. CORS请求在Firefox上没有成功,但在Chrome上有效
  3. Socket.io+Node.js跨源请求被阻止
  4. CORS被node.js和socket.io阻止
  5. Socket.IO:阻止跨来源请求:同源策略不允许读取远程资源

//Firefox客户端错误://阻止跨来源请求:同源策略不允许读取http://<MY_EXTERNAL_IP>:3000/插座.io/?EIO=3&/transport=轮询&t=NMX8aWc。(原因:CORS请求未成功(

在您的app.js或index.js中更新1 将置于代码下方

app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
res.header("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0");
next();
});

如果你正在使用firefox,请参阅以下答案:堆叠溢出上的答案

如果你还没有在应用程序中使用过,请安装cors

你可以在express中使用cors-npm。

npm install cors

更新app.js

var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())

原始答案试试这个也许对你有用它在我的应用中运行良好

var express = require('express');
var app = express();
var http = require('http').Server(app);
const io = require('socket.io')(http, {
origins: '*:*'
});
http.listen(port);
console.log('Server is running on port:' + port);

最新更新