Skip to content

Instantly share code, notes, and snippets.

@sintaxi
Created March 30, 2011 01:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sintaxi/893682 to your computer and use it in GitHub Desktop.
Save sintaxi/893682 to your computer and use it in GitHub Desktop.
Uploads a file with Node.js. Nothing more.
// to test...
// curl -T path/to/file.tar.gz http://localhost:8000
var http = require('http')
var fs = require('fs')
http.createServer(function(req, rsp){
var file = fs.createWriteStream("mynewfile")
file.on("drain", function(){
req.resume()
})
req.on("data", function(chunk) {
req.pause()
file.write(chunk, "binary")
})
req.on("end", function(){
file.end()
rsp.writeHead(200)
rsp.end("it worked!")
})
}).listen(8000)
// same as above
var http = require('http')
var fs = require('fs')
var util = require('util')
http.createServer(function(req, rsp){
var file = fs.createWriteStream("mynewfile")
util.pump(req, file)
req.on("end", function(){
file.end()
rsp.writeHead(200)
rsp.end("done!")
})
}).listen(8001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment