【问题标题】:Cannot get /socket.io/socket.io.js on client application无法在客户端应用程序上获取 /socket.io/socket.io.js
【发布时间】:2017-08-28 21:35:28
【问题描述】:

我的服务器端应用程序托管在herokuhttps://shielded-dusk-72399.herokuapp.com/

相关代码如下:

const
    express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    port = process.env.PORT || 8080

server.listen(port, (err) => {
  console.log(err || 'listening on my very special port ' + port)
})

在我的 heroku 日志中,这会被记录:"listening on my very special port 28428"

现在在我的客户端应用程序上:

目前我的客户端应用程序正在端口 8080 上运行

在我的index.html

<script>/socket.io/socket.io.js" charset="utf-8"></script>

当我转到localhost:8080 时,我收到以下错误:

Cannot GET http://localhost:8080/socket.io/socket.io.js

其他尝试:

<script src="https://shielded-dusk-72399.herokuapp.com:28428/socket.io/socket.io.js" charset="utf-8"></script>

Cannot GET https://shielded-dusk-72399.herokuapp.com:28428/socket.io/socket.io.js net::ERR_CONNECTION_REFUSED

<script src="https://shielded-dusk-72399.herokuapp.com:8080/socket.io/socket.io.js" charset="utf-8"></script>

Cannot GET https://shielded-dusk-72399.herokuapp.com:8080/socket.io/socket.io.js net::ERR_CONNECTION_REFUSED

然后我将./node_modules/socket.io-client/dist/socket.io.js 复制到resources/js 并收到此错误:

Cannot GET http://localhost:8080/resources/js/socket.io.js 404 (Not Found)

有什么想法吗?

我已将这些都用作参考,但仍然不足:

node.js /socket.io/socket.io.js not found

Node.js /socket.io/socket.io.js not found express 4.0

Can't get socket.io.js

【问题讨论】:

  • 哦,谢谢。我添加了一个端口号 而不是

标签: javascript node.js express socket.io


【解决方案1】:

您需要指定从何处提供静态 js 文件。例如,如果您将 socket.io.js 文件放在您的 /resources/js/ 文件夹中,请添加以下内容:

app.use('/resources', express.static(path.join(__dirname, 'resources')))

然后您的文件将在http://localhost:8080/resources/js/socket.io.js 上可用

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多