无法在客户端应用程序

时间:2017-04-03 21:18:28

标签: javascript node.js express socket.io

我的服务器端应用程序托管在heroku上:https://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

1 个答案:

答案 0 :(得分:0)

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

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

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

上提供