Created
February 26, 2019 08:34
-
-
Save athlonUA/78a5137d3adf8ce0e4f3af7aabe8c57f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const http = require('http'), | |
fs = require('fs'); | |
http.createServer(function(request, response) { | |
const filePath = 'test.wav'; | |
const stat = fs.statSync(filePath); | |
const fileSize = stat.size; | |
const range = request.headers.range; | |
if (range) { | |
const parts = range.replace(/bytes=/, '').split('-'); | |
const start = parseInt(parts[0], 10); | |
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1; | |
const chunksize = end - start + 1; | |
const readStream = fs.createReadStream(filePath, { start, end }); | |
const head = { | |
'Content-Range': `bytes ${start}-${end}/${fileSize}`, | |
'Accept-Ranges': 'bytes', | |
'Content-Length': chunksize, | |
'Content-Type': 'audio/mpeg' | |
}; | |
response.writeHead(206, head); | |
readStream.pipe(response); | |
} else { | |
const readStream = fs.createReadStream(filePath); | |
const head = { | |
'Content-Type': 'audio/mpeg', | |
'Content-Length': fileSize | |
}; | |
response.writeHead(200, head); | |
readStream.pipe(response); | |
} | |
}).listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment