I was learning NodeJS and Socket.IO via following code from a book(Learning Node). The example worked. But I want to know how the node is serving the socket.io.js file in the statment <script src="/socket.io/socket.io.js"></script>
Because there is no folder named socket.io in the project root and I don't have any code written to server static file. Is this done via Socket.IO module? Will it conflict if I use express to serve static files?
Client Side code
<html lang="en">
<head>
<meta charset="utf-8">
<title>bi-directional communication</title>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8124');
socket.on('news', function (data) {
var html = '<p>' + data.news + '</p>';
document.getElementById("output").innerHTML=html;
socket.emit('echo', { back: data.news });
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
server side code
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
var counter;
app.listen(8124);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
counter = 1;
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { news: 'world' });
socket.on('echo', function (data) {
if (counter <= 50) {
counter++;
console.log(data.back);
socket.emit('news', {news: data.back});
}
});
});