简体   繁体   English

Azure-Node.js socket.io-Cors

[英]Azure - Node.js socket.io - cors

I create a basic nodejs website on azure last days. 我最近几天在azure上创建了一个基本的nodejs网站。 I would like to have a server dedicate for nodejs, and on the other hand I have a classic HTML5 page on another domain. 我希望有一个专用于nodejs的服务器,另一方面,我在另一个域上有一个经典的HTML5页面。

The matter is that I have a CORS (cross origin problem) when my page try to connect to the azure website. 问题是,当我的页面尝试连接到azure网站时,我遇到了CORS(跨源问题)。 Any solutions ? 有什么解决办法吗? This is a bit of my code : 这是我的代码:

Server : 服务器:

var express = require('express');
var path = require('path');
var app = express();

var server = require('http').createServer(app).listen(process.env.PORT || 5000);
var io = require('socket.io').listen(server);

Client : 客户:

var socket = io.connect('https://****.azurewebsites.net/');

Thanks for your time, 谢谢你的时间,

Regards. 问候。

From my experience the easiest way to enable CORS is with cors npm package . 根据我的经验,启用CORS的最简单方法是使用cors npm package

Include cors in your package.json or run $ npm install --save cors . 在您的package.json中包含cors或运行$ npm install --save cors

Then include the following in your server code. 然后在服务器代码中包含以下内容。

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

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

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