Name: js-handler/node_modules/restify/node_modules/formidable/example/post.js
| 1: | require('../test/common'); |
| 2: | var http = require('http'), |
| 3: | util = require('util'), |
| 4: | formidable = require('formidable'), |
| 5: | server; |
| 6: | |
| 7: | server = http.createServer(function(req, res) { |
| 8: | if (req.url == '/') { |
| 9: | res.writeHead(200, {'content-type': 'text/html'}); |
| 10: | res.end( |
| 11: | '<form action="/post" method="post">'+ |
| 12: | '<input type="text" name="title"><br>'+ |
| 13: | '<input type="text" name="data[foo][]"><br>'+ |
| 14: | '<input type="submit" value="Submit">'+ |
| 15: | '</form>' |
| 16: | ); |
| 17: | } else if (req.url == '/post') { |
| 18: | var form = new formidable.IncomingForm(), |
| 19: | fields = []; |
| 20: | |
| 21: | form |
| 22: | .on('error', function(err) { |
| 23: | res.writeHead(200, {'content-type': 'text/plain'}); |
| 24: | res.end('error:\n\n'+util.inspect(err)); |
| 25: | }) |
| 26: | .on('field', function(field, value) { |
| 27: | console.log(field, value); |
| 28: | fields.push([field, value]); |
| 29: | }) |
| 30: | .on('end', function() { |
| 31: | console.log('-> post done'); |
| 32: | res.writeHead(200, {'content-type': 'text/plain'}); |
| 33: | res.end('received fields:\n\n '+util.inspect(fields)); |
| 34: | }); |
| 35: | form.parse(req); |
| 36: | } else { |
| 37: | res.writeHead(404, {'content-type': 'text/plain'}); |
| 38: | res.end('404'); |
| 39: | } |
| 40: | }); |
| 41: | server.listen(TEST_PORT); |
| 42: | |
| 43: | console.log('listening on http://localhost:'+TEST_PORT+'/'); |
