[英]CORS Blocked with node.js and socket.io
我最近開始學習 node.js 和 socket.io。 我遵循了 socket.io 的一個簡單教程,並且在我的計算機上運行時一切正常。 但是,我決定將客戶端部分上傳到服務器進行測試,這就是問題開始的地方。 我想在網絡主機上運行聊天客戶端,並在我的計算機或其他主機上運行服務器。 基本上,我計划端口轉發服務器,並讓客戶端在網頁上運行。 我打開了我的端口以進行端口轉發,它似乎可以正常工作,但是我每次都在網頁上收到錯誤消息。
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://24.151.51.34:3000/socket.io/?EIO=3&transport=polling&t=1437399007343-0. (Reason: CORS request failed).
我一直在搞亂代碼,希望在開始我自己的項目之前找到解決這個問題的方法,但是我想不出辦法。 客戶端代碼是:
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io('24.151.51.34:3000');
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
對於服務器代碼,我嘗試添加代碼以允許 CORS,但真的不知道該怎么做:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.set('origins', 'http://browsercombat.com:80');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
app.get('/', function(req, res, next) {
res.sendFile(__dirname + '/index.html');
});
app.post('/', function(req, res, next) {
// Handle the post for this route
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
除了使用 cors 包外,我還通過向 socketio 創建添加選項來修復它。 從版本 3 cors 配置開始,您必須添加這些選項。 也適用於套接字版本 2.xx。
const io = socketio(httpServer, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
credentials: true
}
});
這是資源https://socket.io/docs/v3/handling-cors/
獎勵:如果您遇到錯誤請求,請確保客戶端和服務器具有相同版本的 socket.io。
我設法通過使用有人推薦我的這個 CORS 中間件並檢查我的防火牆設置來解決這個問題。 https://www.npmjs.com/package/cors
我現在正在使用 npm cors 包,它就像一個魅力一樣工作 socket.io 也沒有問題。
像這樣添加它 app.options('*', cors());
這意味着 Access-Control-Allow-Origin: '*'
var express = require('express');
var app = express();
var http = require('http').Server(app);
const cors = require('cors');
var bodyParser = require('body-parser');
app.use(cors());
app.options('*', cors());
const auth = require('./routes/auth');
const port = 3004;
http.listen(port, () => {
console.log( `running http at port ${port}`);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.