NodeJs response client's url request to another

I'm managing one kind of client request starts from '/js/' which means root folder on server side or maybe will be located in '/public/js/' for further use. Okay, the problem is I got this message:

//server side
GET /js/socket.io/socket.io.js 304 7ms  

// client side 
Uncaught ReferenceError: require is not defined     
Uncaught ReferenceError: io is not defined 

[x] source

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

// app.js   
app.get('/js/*', function (req, res) {
    res.sendfile(__dirname + req.url.substr('/js'.length));
});

But it does work when I change to fixed path.

[o] source

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

// app.js   
app.get('/socket.io/socket.io.js', function (req, res) {
    res.sendfile(__dirname + '/socket.io/socket.io.js');
});

It's kind of weird, isn't it? Could anybody figure out where is wrong plz.

Thanks a lot.

What's your exact problem with that response?

A 304 status code means Not modified, meaning that your browser can used the cached version of /js/socket.io/socket.io.js instead of the server having to send it to the browser again (therefore, saving bandwidth).

When you require socket.io in your application, it sets a handler to intercept requests for /socket.io/socket.io.js (source). That is why the second example works.

In fact, you can remove these lines in app.js:

app.get('/socket.io/socket.io.js', function (req, res) {
  res.sendfile(__dirname + '/socket.io/socket.io.js');
});