简体   繁体   English

Node.js如何将链接标记解析为节点模块

[英]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. 我正在学习在节点js中使用socket io,我遇到了这个让我烦恼的例子。 I am following the example at http://socket.io/ : client side: 我在http://socket.io/上的示例:客户端:

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

my question is how this src url gets resolved by node js? 我的问题是如何通过节点js解析这个src url? 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. 我从未将socket.io.js复制到任何公共/视图文件夹中。 It seems there is some rule that node can pull js file directly from node modules like magic. 似乎有一些规则,节点可以直接从像魔术这样的节点模块中提取js文件。 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. Socket.io向您的服务器添加一个连接侦听器,该侦听器为其客户端JS提供响应该URL的连接。
See the documentation . 请参阅文档

To disable this, set the browser client config option to false. 要禁用此功能,请将browser client 配置选项设置为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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