Skip to content

Instantly share code, notes, and snippets.

@geta6
Created September 27, 2012 10:13
Show Gist options
  • Save geta6/3793279 to your computer and use it in GitHub Desktop.
Save geta6/3793279 to your computer and use it in GitHub Desktop.
response partial content in node.js
#
# blob : Object from MongoDB
#
# blob.body: Buffer
# blob.size: length of buffer, substitute for blob.body.length
# blob.type: MIME (Eg. audio/x-wav)
#
# req : Object from http
# res : Object from http
# _ : Object from underscore
#
if blob
range = req.headers.range
unless range
res.writeHead 200,
'Content-Type': blob.type
res.end blob.body
else
[ini, end] = _.map range.replace(/bytes=/, '').split('-'), (n) -> parseInt n, 10
total = blob.size
chunk = end - ini + 1
#console.log "#{ini}-#{end}/#{total} #{chunk}".cyan
res.writeHead 206,
'Content-Type': blob.type
'Content-Length': chunk
'Content-Range': "bytes #{ini}-#{end}/#{total}"
'Accept-Ranges': 'bytes'
res.end blob.body
else
res.writeHead 404
res.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment