Javascript 为什么我会得到这个';无法加载资源';错误?
我正在尝试创建一个带有socket.io和node.js的聊天应用程序 我正在学习socket.io网站上的教程,但我无法获取控制台日志 这是我的密码: index.jsJavascript 为什么我会得到这个';无法加载资源';错误?,javascript,socket.io,Javascript,Socket.io,我正在尝试创建一个带有socket.io和node.js的聊天应用程序 我正在学习socket.io网站上的教程,但我无法获取控制台日志 这是我的密码: index.js const app = express(); const http = require('http').createServer(app); const io = require('socket.io')(http); app.get('/', (req, res) => { res.sendFile(__dirn
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', socket => {
console.log('a user connected');
});
http.listen(3000, () => console.log('Listening to 3000'));
index.html
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: 13px Helvetica, Arial;
}
form {
background: #000;
padding: 3px;
position: fixed;
bottom: 0;
width: 100%;
}
form input {
border: 0;
padding: 10px;
width: 90%;
margin-right: 0.5%;
}
form button {
width: 9%;
background: rgb(130, 224, 255);
border: none;
padding: 10px;
}
#messages {
list-style-type: none;
margin: 0;
padding: 0;
}
#messages li {
padding: 5px 10px;
}
#messages li:nth-child(odd) {
background: #eee;
}
</style>
<script src="/socket.io/socket.io.js"></script>
<script src="client.js"></script>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>
如果我把这一行(const socket=io();)放在.html中的脚本标记中,它就会工作。但我不想这么做。尝试添加app.use(“/scripts”,express.static(\uu dirname+“/node\u modules/socket.io”)代码>到您的代码
然后,从/scripts/socket.io.js加载脚本。
希望这能帮助或引导你朝着正确的方向前进
const socket = io('http://localhost:3000');