【问题标题】:NodeJS Socket io client side script not found找不到NodeJS Socket io客户端脚本
【发布时间】:2018-08-23 09:05:54
【问题描述】:

NodeJS 9.8.0
找不到 NodeJS 客户端 socket.io 脚本。 包.json:

..."dependencies": {
    "express": "latest",
    "socket.io": "latest"
}...

index.js:

var http = require("http"),
    express = require("express"),
    io = require("socket.io").listen(http),
    fs = require("fs");

var app = express();

var web = __dirname.split("\\").join("/")+"/web/";

app.get("/", function(req, res)
{
    res.sendFile(web + "index.html");
});
app.get("/:file", function(req, res)
{
    var file = web + req.param("file");
    var err = web + "error.html";
    fs.exists(file, function(exist)
    {
        if (exist) res.sendFile(file);
        else fs.exists(err, function(exist)
        {
            if (exist) res.sendFile(err);
            else res.send("<h1>Error! Page not found!</h1>");
        });
    });
});
// app.get("/*", function(req, res)
// {
    // var err = web + "error.html";
    // res.sendFile(err);
// });

io.sockets.on("connection", function(socket)
{
    console.log(socket);
});

http.createServer(app).listen(8080);

index.html:

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

和错误:socket.io.js 加载资源失败:服务器响应状态为 404(未找到)

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:

    如果您转到http://socket.io/socket.io.js,它会显示 404。脚本已被移动或被删除。

    这里有下载脚本的链接https://cdnjs.com/libraries/socket.io

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

    告诉你的浏览器去socket.io并获取socket.io.js(使用http或https),这取决于你所在的网页是如何加载的。

    我不确定 socket.io 是否应该用某些东西代替它,但我根据阅读他们的教程对此表示怀疑。

    如果您访问 github 示例,您会看到他们将其更改为

     <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    

    https://github.com/socketio/chat-example/blob/master/index.html

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2016-03-10
      • 2021-10-05
      • 2020-02-23
      相关资源
      最近更新 更多