How does Node serve client .js files?

Problem

For example, let's say we're trying to use socket.io.

In the html file served to the client, we include the following in the file.

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

So two questions regarding this:

  1. When the html file gets served to the client, it loads the socket.io.js resource (http://localhost:8080/socket.io/socket.io.js) without triggering the requestHandler in server.js (I have a log statement for any request that hits localhost:8080). How does this resource load on the client without triggering the requestHandler?

  2. Where does Node find the socket.io.js resource that is required by the client?

Problem courtesy of: Mark

Solution

Assuming you are using one of the basic Socket.io examples, this is because Socket.io overrides your handle with it's own and won't run your handler if the request is for something that socket.io manages.

When you call .listen(app) or .listen(80) it will set up all of the handlers it needs to process data and serve the client JS file.

Solution courtesy of: loganfsmyth

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.