'apache node js socket.io error connection refused

There is a php-file on apache:

<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);
});

and node js in same directory

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');
   });
   
});

browser throws an error: GET http://192.168.100.31/socket.io/?EIO=4&transport=polling&t=NxOsf3z 404 (Not Found)

if replace let socket = io.connect( 'http://localhost:3000' ) on let socket = io.connect( 'http://192.168.100.31:3000' ); browser will give another error: Access to XMLHttpRequest at 'http://192.168.100.31:3000/socket.io/?EIO=4&transport=polling&t=NxOt0X0' from origin 'http://192.168.100.31' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source