Last active
November 23, 2024 18:21
-
-
Save DingGGu/8144a2b96075deaf1bac to your computer and use it in GitHub Desktop.
NodeJS Mp3 Streaming ExpressJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var fs = require('fs'); | |
app.listen(3000, function() { | |
console.log("[NodeJS] Application Listening on Port 3000"); | |
}); | |
app.get('/api/play/:key', function(req, res) { | |
var key = req.params.key; | |
var music = 'music/' + key + '.mp3'; | |
var stat = fs.statSync(music); | |
range = req.headers.range; | |
var readStream; | |
if (range !== undefined) { | |
var parts = range.replace(/bytes=/, "").split("-"); | |
var partial_start = parts[0]; | |
var partial_end = parts[1]; | |
if ((isNaN(partial_start) && partial_start.length > 1) || (isNaN(partial_end) && partial_end.length > 1)) { | |
return res.sendStatus(500); //ERR_INCOMPLETE_CHUNKED_ENCODING | |
} | |
var start = parseInt(partial_start, 10); | |
var end = partial_end ? parseInt(partial_end, 10) : stat.size - 1; | |
var content_length = (end - start) + 1; | |
res.status(206).header({ | |
'Content-Type': 'audio/mpeg', | |
'Content-Length': content_length, | |
'Content-Range': "bytes " + start + "-" + end + "/" + stat.size | |
}); | |
readStream = fs.createReadStream(music, {start: start, end: end}); | |
} else { | |
res.header({ | |
'Content-Type': 'audio/mpeg', | |
'Content-Length': stat.size | |
}); | |
readStream = fs.createReadStream(music); | |
} | |
readStream.pipe(res); | |
}); |
Great, I have the question about statSync. Can I use only url link instead of local File mp3?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work!