Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么我会收到此错误消息;网络::错误“连接被拒绝”吗;?_Node.js_Express_Socket.io - Fatal编程技术网

Node.js 为什么我会收到此错误消息;网络::错误“连接被拒绝”吗;?

Node.js 为什么我会收到此错误消息;网络::错误“连接被拒绝”吗;?,node.js,express,socket.io,Node.js,Express,Socket.io,我已经在我的计算机上安装了node.js,并且在我的计算机上用两个打开的web浏览器成功地测试了这段代码 问题是,代码在web服务器上不起作用。我真的不知道为什么 代码来自此YouTube播放列表: index.html <html> <head> <meta charset="UTF-8"> <title>Sockets Example 1</title> <script type="text/javascrip

我已经在我的计算机上安装了node.js,并且在我的计算机上用两个打开的web浏览器成功地测试了这段代码

问题是,代码在web服务器上不起作用。我真的不知道为什么

代码来自此YouTube播放列表:

index.html

<html>
<head>
  <meta charset="UTF-8">
  <title>Sockets Example 1</title> 
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
  <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
  <script language="javascript" type="text/javascript" src="sketch.js"></script>
</head>

<body>
</body>
</html>
server.js

var socket;

function setup() {
    createCanvas(200,200);
    background(51);

    //socket = io.connect('http://127.0.0.1:3000'); ?
    socket = io.connect('http://localhost:3000');       

    socket.on('mouse', newDrawing);
}

function newDrawing(data) {
    noStroke();
    fill(255, 0, 100);
    ellipse(data.x, data.y, 36, 36);            
}

function mouseDragged() {
    console.log('Sending: ' + mouseX + ',' + mouseY);       

    var data = {
        x: mouseX,
        y: mouseY
    }
    socket.emit('mouse', data);     

    noStroke();
    fill(255);
    ellipse(mouseX, mouseY, 36, 36);
}
var express = require('express');

var app = express();
var server = app.listen(3000);

app.use(express.static('public'));

console.log("My socket server is running!");

var socket = require('socket.io');

var io = socket(server);

io.sockets.on('connection', newConnection);

function newConnection(socket) {
    console.log('new connection: ' + socket.id);

    socket.on('mouse', mouseMsg);   

    function mouseMsg(data) {
        socket.broadcast.emit('mouse', data);
    }   
}
package.json

{
  "name": "sockets_example_1",
  "version": "1.0.0",
  "description": "sockets example 1",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "keywords": [
    "sockets",
    "p5.js",
    "node"
  ],
  "author": "Noa",
  "license": "No license",
  "dependencies": {
    "express": "^4.17.1",
    "socket.io": "^2.2.0"
  }
}
这是文件夹结构:

├───node_modules
│   └───... (created files with node.js on my computer)
├───public
│   ├───index.html
│   └───sketch.js
├───package.json
├───package-lock.json
└───server.js
在chrome web浏览器中,我收到以下错误消息:

index.js:83 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MqfwDDZ net::ERR_CONNECTION_REFUSED
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MqfvjtX. (Reason: CORS request did not succeed).
在firefox web浏览器中,我收到以下错误消息:

index.js:83 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MqfwDDZ net::ERR_CONNECTION_REFUSED
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MqfvjtX. (Reason: CORS request did not succeed).

在我的web服务器上安装了node.js。

您需要使用web服务器的IP而不是
io.connect()中的本地主机进行连接。
连接被拒绝的错误可能有多种原因。请参考这个链接@DragonBorn:我使用了一个在线“域到IP转换器”。已将本地主机更改为IP。不工作。这是现代浏览器的安全限制,当你运行JS代码时,它会在用户机器上本地运行。这会带来安全风险,因为您的网站可以是www.google.co.uk,但您可以有效地运行JS代码,让用户打开任何其他网站,如www.mybadwebsite.com-如果您向其发送请求的网站与用户所在的来源不匹配,则将被阻止,除非标题请求和响应允许交叉来源。您需要在
io.connect()
中使用Web服务器的IP而不是本地主机连接。连接被拒绝的错误可能有多种原因。请参考这个链接@DragonBorn:我使用了一个在线“域到IP转换器”。已将本地主机更改为IP。不工作。这是现代浏览器的安全限制,当你运行JS代码时,它会在用户机器上本地运行。这会带来安全风险,因为您的网站可以是www.google.co.uk,但您可以有效地运行JS代码,让用户打开任何其他网站,如www.mybadwebsite.com-如果您向其发送请求的网站与用户所在的来源不匹配,则该网站将被阻止,除非标题请求和响应允许跨来源。