Skip to content

Instantly share code, notes, and snippets.

@dyoder
Created March 17, 2013 03:46
Show Gist options
  • Save dyoder/5179473 to your computer and use it in GitHub Desktop.
Save dyoder/5179473 to your computer and use it in GitHub Desktop.
Demonstration of Node's support for chunked encoding.
http = require "http"
fs = require "fs"
app = (request,response) ->
n = 0
out = fs.createWriteStream "./test.png"
request.pipe out
request.on "data", (data) ->
console.log "CHUNK #{++n}: #{data.length} bytes"
request.on "end", ->
response.statusCode = 200
response.write "OK"
response.end()
http.createServer(app).listen(1337,"127.0.0.1").on "listening", ->
console.log "Listening on port 1337"
@dyoder
Copy link
Author

dyoder commented Mar 17, 2013

Node's HTTP implementation handles chunked encoded requests by default. The request is already a stream so your code doesn't need to change to handle stream based data. Here's a simple demonstration that writes the request data to a file. Try it from curl with or without transfer-encoding.

curl -H 'transfer-encoding: chunked' --data-binary @<your-large-PNG-file> http:/ocalhost:1337

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment