在 node.js 上使用 socket.io 时如何处理 CORS

可爱的马歇尔

我目前正在学习如何使用 Node 创建聊天应用程序并为后端表达并为前端做出反应,但遇到 CORS 错误。

由于 CORS,我在下面收到此错误,但是,我已经在应用程序上实现了所有可以实现的 CORS。

这是错误

:3001/chat?name=jaden&room=love:1 Access to XMLHttpRequest at 'http://localhost:8000/socket.io/?EIO=4&transport=polling&t=NOYd8LK' from origin 'http://127.0.0.1:3001' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3001' that is not equal to the supplied origin.
polling-xhr.js:203 GET http://localhost:8000/socket.io/?EIO=4&transport=polling&t=NOYd8LK net::ERR_FAILED
create @ polling-xhr.js:203
Request @ polling-xhr.js:120
request @ polling-xhr.js:63
doPoll @ polling-xhr.js:92
poll @ polling.js:76
doOpen @ polling.js:23
open @ transport.js:43
open @ socket.js:170
Socket @ socket.js:94
push../node_modules/component-emitter/index.js.Emitter.emit @ index.js:145
onError @ polling-xhr.js:246
(anonymous) @ polling-xhr.js:196
setTimeout (async)
xhr.onreadystatechange @ polling-xhr.js:195
:3001/chat?name=jaden&room=love:1 Access to XMLHttpRequest at 'http://localhost:8000/socket.io/?EIO=4&transport=polling&t=NOYd9p5' from origin 'http://127.0.0.1:3001' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3001' that is not equal to the supplied origin.
polling-xhr.js:203 GET http://localhost:8000/socket.io/?EIO=4&transport=polling&t=NOYd9p5 net::ERR_FAILED

这是后端代码

const express = require('express')
const http = require('http')
const socketIo = require('socket.io')
const cors = require('cors')

const app = express()

app.use(cors())

const server = http.createServer(app)

const io = socketIo(server, {
  cors: {
        origin: "http://localhost:3001",
        methods: ["GET", "POST"]
      }
})

// Listening for a connection on the socket.io server

io.on('connection', (socket)=>{
  // the moment i get a connection, i want to send a welcome message
  socket.on('join', ({name, room})=>{
    socket.emit('message', {user:'admin', text:`${name}, You're welcome!`})
    socket.broadcast.emit('message', {user:'admin', text:`${name}, just joined`})
    console.log('welcome message')
  })

  socket.on('sendMessage', (message)=>{
    io.emit('message', {user:'user', text:message})
  })

  socket.on("disconnect", ()=>{
    io.emit('message', {user:'admin', text:`user Just left!`})
  })

})


const port = process.env.PORT || 8000
server.listen(port, ()=> console.log(`Listening on port : ${port}`))

阿比尔·塔希尔

本地主机不是实际的来源。源 ip 地址是 127.0.0.1,因此将源从 更改http://localhost:3001http://127.0.0.1:3001

const io = socketIo(server, {
      cors: {
        origin: "http://127.0.0.1:3001",
        methods: ["GET", "POST"]
      }
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用SSL的node.js,socket.io

如何使用Express / Socket.io在Node.js上使用HTTPS

socket.io/socket.io.js卡在(待处理)-Node.js

如何在socket.io和Node.js中使用Asterisk ARI

如何使用连接到套接字的socket.id发送多个客户端(Node.js,Socket.io)

如何在客户端上导入socket.io-SOCKET.IO + NODE.JS

使用socket-io Node.js处理生成(启动,终止)的作业

如何使用node.js或socket.io获得内存利用率

如何使用socket.io从Express Node.js服务器发送数据

Node.js,socket.io和mongojs-使用socket.io登录表单

使用不带端口的Node js库Socket io

Node.js Socket.IO客户端``未处理的socket.io URL''

在Node.js中使用socket.io的奇怪错误

如何使用Express和Socket.IO设置Node.JS?

如何避免使用socket.io在JS上使用this = this?

为node.js安装socket.io时出错

如何在Node.js中使用承载令牌实现socket.io身份验证

如何在Node.js上安装socket.io和mysql

在openshift上使用socket.io和node.js时,即使指定了端口8000,也无法连接到端口8080错误

使用node.js和socket.io的私人聊天

Node.JS socket io 连接

如何使用 SSL 域运行 NODE.JS (Socket.io) - CENTOS 7?

如何在 node.js 中全局使用 socket.io

Socket.io 和 node JS 导致 CORS 阻塞问题

require 函数如何使用 Socket.io 在 Node.js 中获取参数

Node.js:在集群中使用 socket.io

在Node.js上处理并发请求时,如何处理清楚?

如何在 node.js worker_threads 中使用 socket.io

Socket.io + Node.js CORS 错误阻止了我的请求