1

I just played around with a hello-world-chat express app (here). And I noticed the following in the index.html

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

I had installed

npm install --save socket.io

How does express know where it is, because there is no file like

/node_modules/socket.io/socket.io.js

For example, I would like to do the same with jquery

$> npm install --save jquery

<script src="/jquery/jquery.js"></script>

However, this doesn't work :(

Can someone explain to me how this works ? And if possible how I can fix this for jquery.

1
  • 1
    When you require socket.io and attach it to your server, that module injects it's own handling into the server so that it will serve that static file. jQuery doesn't do that automatically, so you'll have to do that yourself using express, or by copying it into a public folder being served by express static. the latter is the normal way of doing this, usually combined with a build tool such as gulp or grunt. Commented Aug 18, 2015 at 20:23

1 Answer 1

4

When you install the server-side socket.io library and then initialize socket.io server-side, you pass it your express app object. It then installs a route handler for the/socket.io/socket.io.js route and when that route is requested, that handler serves up the appropriate client file from it's own socket.io module. The client file is not at this exact path and does not necessarily even have this filename, but when that route is hit, the server-side socket.io installed route will serve up the appropriate file for client-side socket.io support.

There is a nice advantage to doing it this way because any time you upgrade your server-side socket.io library, you will automatically get a new client-side library (embedded in the server-side library) that will always keep the two in sync. If you served a separate client-side library (from a CDN or from your own route), you'd have to make sure you upgraded client and server libraries at the same time.


If you want to serve up jQuery from your server, then you can either use express.static() to put the jQuery file in a known location on your server and use the express.static() configuration to point a specific route to the right location. See Serving Static Files in Express for documentation.

Or, if you really want to do it like socket.io does it, then you can install your own route for /jquery/jquery.js and then you serve up the jQuery file (from wherever you put it on your server) whenever that route is hit. For example, here's a custom route to serve up a specific version of jQuery from where it gets installed with the command you mentioned npm install --save jquery:

app.get('/jquery/jquery.js', function(req, res) {
    res.sendfile(__dirname + '/node_modules/jquery/dist/jquery.min.js');
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.