I have a little express server that either downloads or streams an mp3 file, which looks like this:
const express = require('express');
const fs = require('fs');
const app = express();
app.use('/mp3', express.static(__dirname + '/mp3'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/stream', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
fs.exists(file, (exists) => {
if (exists) {
const rstream = fs.createReadStream(file);
rstream.pipe(res);
} else {
res.send('Error - 404');
res.end();
}
});
});
app.get('/download', (req, res) => {
const file = __dirname + '/mp3/trololol.mp3';
res.download(file);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'));
html:
<audio controls="controls">
<source src="http://localhost:3000/stream" type="audio/ogg" />
<source src="http://localhost:3000/stream" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
This works, however, the audio stream does not rewind or fast forward. Do I have to change something in the request headers to allow this to happen? Perhaps I need to set ranges and add start and end times or something. Any tip would be appreciated. Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…