【问题标题】:nodejs and socket.io.js path resolutionnodejs 和 socket.io.js 路径解析
【发布时间】:2021-05-14 13:12:27
【问题描述】:

我刚刚开始使用 socket.io.js 库学习 nodejs。我的问题与这些库中的内容无关,而是与访问浏览器如何提供文件有关。

在我的服务器目录中,只有 2 个文件(index.html 和 server.js)以及 node_modules 目录(用于 socket.io)。在 index.html 中,我有一个包含客户端 socket.io 库的脚本标记,如下所示,

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

相关的服务器代码是,

var server = http.createServer(
    function(req, res) {
        res.writeHead(200, { 'Content-type': 'text/html'});
        res.end(fs.readFileSync(__dirname + '/index.html'));
    }
    ).listen(8080, 
    function() {
        console.log('Listening at: http://localhost:8080');
    }
);

我的问题是该文件在服务器上的什么位置(存在 index.html 的目录中没有 socket.io 目录)?那么网络浏览器如何以及从何处正确解析和下载呢?

抱歉这个菜鸟问题。

【问题讨论】:

  • 您的服务器代码缺少socket.io 的服务器端设置。见this page
  • 哎呀,代码远低于我看到的示例代码,无论如何,phonicx 的响应帮助我理解了它是如何工作的。

标签: node.js socket.io httpserver


【解决方案1】:

客户端文件由 socket.io npm 模块自动注入,因此当您升级 npm 模块时,您的客户端版本的 socket.io 会自动更新。

实际文件位于:

/usr/local/lib/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js

编辑:忘了提到,当你初始化 socket.io 时,你实际上是在让它启动它自己的服务器来提供文件。

【讨论】:

    猜你喜欢
    • 2015-04-07
    • 2019-02-23
    • 2012-09-06
    • 1970-01-01
    • 2019-01-13
    • 2013-07-18
    • 1970-01-01
    • 2014-12-16
    • 2020-06-29
    相关资源
    最近更新 更多