i have got a problem with socket IO connection from the page, witch geneerated by nodeJs server, to another nodeJs server listening by Socket.IO
The idea is contains of 2 tasks: Generating HTML templates by one Express server Support socket io by another express server
Here is server.js code:
const app = require('express');
const util = require('util');
const http = require('http');
const fs = require('fs');
const debug = console.log;
const moment = require('moment');
const event = require('events').EventEmitter;
var server = app();
server.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
}
);
server.use(app.json());
server.use(app.urlencoded());
server.use(app.logger());
server.use(app.favicon());
server.use(app.static(__dirname + '/core'));
server.get('*', function (req, res, next) {
debug('Connection refused: \t' + req.url);
var r = (e.url.split('/')[2] == 'socket.io.js') ? server.st.socket : server.st.tmp; // Return TMP-HTML FILE OR SOCKET.IO JS FILE FOR CLIENT
res.send(r);
res.end();
});
server.st = {
socket: fs.readFileSync('./socket.io.js', 'utf-8')
tmp: fs.readFileSync('./tmp.html', 'utf-8')
}
server.listen(81);
*******************************************************************************
//Creating SOCKET server
io = require('socket.io').listen(82);
io.configure(function () {
io.set('log level', 1);
io.set('origin', '*');
});
io.sockets.on('connection', function (socket) {
debug('IT IS WORK')
})
Here Is tmp.html code:
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:82');
</script>
</head>
<body>
</body>
And default Socket.io.js for client
And here is Error on client side
XMLHttpRequest cannot load http://localhost:82/socket.io/?EIO=2&transport=polling. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access.
What's wrong with this *?