2

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.

2
  • Your server code is missing the server-side setup of socket.io. See this page. Commented Apr 10, 2013 at 15:33
  • oops that code was way below the example code I saw it in, anyhow phonicx's response helped me understand how it works. Commented Apr 10, 2013 at 15:41

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.