我使用网络套接字创建了一个聊天应用程序。我也在 heroku 上部署了它,但 client.js 文件无法正常工作。
Maybe the problem with the process.env.PORT but not sure.
客户端.js:
const socket = io('http://localhost:3000');
Error:client.js:2 Uncaught ReferenceError: io is not defined
at client.js:2
这是 index.js 文件
const express=require('express');
var app=express();
const PORT = process.env.PORT || 3000;
const socketIO=require('socket.io');
var path=require('path');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(__dirname+'/public'));
app.get('/',function(req,res){
res.sendfile(__dirname+'/index.html');
})
var server=app.listen(PORT,()=>{
console.log(`Listening on ${PORT}`);
})
Index.html
<script defer src="http://localhost:3000/socket.io/socket.io.js"></script>
<script defer src="./js/client.js"></script>
请帮帮我。