简体   繁体   中英

How does Node.js resolve link tag to node modules

I am learning to use socket io in node js and I came across this example that really bugs me. I am following the example at http://socket.io/ : client side:

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

my question is how this src url gets resolved by node js? I never configured my server to handle this url. Below is my server code snippet. I never copied socket.io.js to any of my public/views folders. It seems there is some rule that node can pull js file directly from node modules like magic. Can any one explain how this works?

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
...

var app = express.createServer();

app.configure(function() {
    app.use(express.logger());
    app.use(express.bodyParser());
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.static(__dirname + '/public'));
    app.use(express.cookieParser());

Socket.io adds a connection listener to your server that serves its client JS in response to that URL.
See the documentation .

To disable this, set the browser client config option to false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM