Skip to content

Instantly share code, notes, and snippets.

@marcmo
Created April 26, 2011 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcmo/942504 to your computer and use it in GitHub Desktop.
Save marcmo/942504 to your computer and use it in GitHub Desktop.
doesn't work with the async stuff in line 23
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