You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

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