Search code examples
node.jssocket.iohttpserver

nodejs and socket.io.js path resolution


I have just started learning nodejs with the socket.io.js library. My question isn't really related to the stuff in these libraries but rather on how the files are served by a visiting browser.

In my server directory there are just 2 files present (index.html and server.js) along with the node_modules directory (for socket.io). In the index.html I have a script tag including the client side socket.io lib as follows,

<script src="/socket.io/socket.io.js"></script>

The relecvant server code is,

var server = http.createServer(
    function(req, res) {
        res.writeHead(200, { 'Content-type': 'text/html'});
        res.end(fs.readFileSync(__dirname + '/index.html'));
    }
    ).listen(8080, 
    function() {
        console.log('Listening at: http://localhost:8080');
    }
);

My question is where is this file present on the server (there is no socket.io directory in the dir where index.html is present)? So how and from where is this being resolved and downloaded correctly by the web browser?

Sorry for the noob question.


Solution

  • The client side file is injected by the socket.io npm module automatically so that when you upgrade the npm module your client side version of socket.io gets updated automatically.

    The actual file lives at:

    /usr/local/lib/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js

    Edit: Forgot to mention that when you initialise socket.io you are actualy making it start its own server that serves the file.