Created
April 26, 2011 15:41
-
-
Save marcmo/942504 to your computer and use it in GitHub Desktop.
doesn't work with the async stuff in line 23
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 http = require('http'), | |
util = require('util'), | |
formidable = require('formidable'), | |
server; | |
global.TEST_PORT = 13532; | |
server = http.createServer(function(req, res) { | |
if (req.url == '/') { | |
res.writeHead(200, {'content-type': 'text/html'}); | |
res.end( | |
'<form action="/post" method="post">'+ | |
'<input type="text" name="title"><br>'+ | |
'<input type="text" name="data[foo][]"><br>'+ | |
'<input type="submit" value="Submit">'+ | |
'</form>' | |
); | |
} else if (req.url == '/post') { | |
console.log('got post request'); | |
var form = new formidable.IncomingForm(), | |
fields = []; | |
req.pause(); | |
process.nextTick(function(){ | |
req.resume(); | |
form | |
.on('error', function(err) { | |
res.writeHead(200, {'content-type': 'text/plain'}); | |
res.end('error:\n\n'+util.inspect(err)); | |
}) | |
.on('field', function(field, value) { | |
fields.push([field, value]); | |
}) | |
.on('end', function() { | |
console.log('on end -> post done'); | |
res.writeHead(200, {'content-type': 'text/plain'}); | |
res.end('received fields:\n\n '+util.inspect(fields)); | |
}); | |
form.parse(req); | |
}); | |
} else { | |
res.writeHead(404, {'content-type': 'text/plain'}); | |
res.end('404'); | |
} | |
}); | |
server.listen(TEST_PORT); | |
util.puts('listening on http://localhost:'+TEST_PORT+'/'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment