apache 节点 js socket.io 错误连接被拒绝

apache node js socket.io error connection refused

apache 上有一个php-file:

<script src="/../node_modules/socket.io/client-dist/socket.io.js"></script>

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

socket.on('update', function updateData(sounds) {
    console.log(sounds);
});

和node js在同一目录

let http = require('http');
let express = require('express');
let path = require('path');
let socketIO = require('socket.io');
let app = express();
let server = http.Server(app);
let io = socketIO(server);

app.set('port', 3000);

server.listen(3000, function() {
   console.log('listening on *:3000');
});

io.on('connection', function(socket) {
   console.log('A user connected');
  
   socket.on('disconnect', function() {
      console.log('A user disconnected');
   });
   
});

浏览器抛出错误:GET http://192.168.100.31/socket.io/?EIO=4&transport=polling&t=NxOsf3z 404(未找到)

if 替换 let socket = io.connect( 'http://localhost:3000' ) 在 让 socket = io.connect( 'http://192.168.100.31:3000' ); 浏览器将给出另一个错误:CORS 策略已阻止从源 'http://192.168.100.31' 访问位于 'http://192.168.100.31:3000/socket.io/?EIO=4&transport=polling&t=NxOt0X0' 的 XMLHttpRequest:请求的资源上不存在 'Access-Control-Allow-Origin' header。

只好更换

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

let socket = io.connect( 'http://192.168.100.31:3000/' );