我有一个HTML页面,其中包含一个标签,可在页面加载时自动播放歌曲(歌曲文件为.mp3)。当我在我的Mac上将其作为文件打开时,它将播放该歌曲,但是,当我使用带有socket.io的node.js文件运行它时,它根本不会播放。
控制台中的错误消息:"无法加载资源:服务器响应状态为404(未找到)"
HTML(没有样式标签)
<!doctype html>
<html>
<head>
<title>Lucas Chat</title>
</head>
<body>
<div id="top">
<h1>Welcome to the chat!</h1>
<audio autoplay>
<source src="onedance.ogg" type="audio/ogg">
</audio>
<br>
<p>
Enter in a nickname and a message to get started
</p>
<br>
<p id="drop">
Scroll up or down to view newer/older messages
</p>
</div>
<ul id="messages"></ul>
<form action="">
<input id="u" autocomplete="off" placeholder="Nickname"/>
<input id="m" autocomplete="off" placeholder="Message"/>
<button>
Send
</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit(function() {
var username = "<span style='font-weight:600;color:#006cff;font-size:18px;'>" + $('#u').val() + "</span>";
var message = "<span style='color:#000000;'>" + $('#m').val() + "</span>";
if ($('#m').val() != '' && $('#u').val() != '') {
socket.emit('send message', {
msg : message,
user : username
});
$('#u').css("display", "none");
$('#m').css("width", "90%");
$("#u").css("box-shadow", "0px 0px 0px rgba(0, 0, 0, 0)");
$("#m").css("box-shadow", "0px 0px 0px rgba(0, 0, 0, 0)");
$('#m').val('');
} else if ($('#u').val() === '' && $('#m').val() === '') {
$("#u").css("box-shadow", "0px 0px 15px #ff8989");
$("#m").css("box-shadow", "0px 0px 15px #ff8989");
} else if ($('#u').val() === '' && $('#m').val() != '') {
$("#u").css("box-shadow", "0px 0px 15px #ff8989");
$("#m").css("box-shadow", "0px 0px 0px rgba(0, 0, 0, 0)");
} else if ($('#m').val() === '' && $('#u').val() != '') {
$("#m").css("box-shadow", "0px 0px 15px #ff8989");
$("#u").css("box-shadow", "0px 0px 0px rgba(0, 0, 0, 0)");
}
return false;
});
socket.on('receive message', function(msg) {
$('#messages').append($('<li>').html(msg));
});
</script>
</body>
</html>
JS
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var peopleCount = 0;
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
peopleCount++;
console.log('a user connected | connections: ' + peopleCount);
socket.on('disconnect', function() {
peopleCount--;
console.log('a user disconnected | connections: ' + peopleCount);
});
});
io.on('connection', function(socket) {
socket.on('send message', function(data) {
io.emit('receive message', data.user + ': ' + data.msg);
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});
答案 0 :(得分:1)
我猜你还没有提供静态文件。快递你需要像
这样的东西 app.use('/static', express.static('public'));
app.js中的任何地方
您的.ogg文件需要位于/ static /.