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.

222 lines
8.3 KiB

  1. Description
  2. ===========
  3. A node.js module for parsing incoming HTML form data.
  4. Requirements
  5. ============
  6. * [node.js](http://nodejs.org/) -- v4.5.0 or newer
  7. Install
  8. =======
  9. npm install busboy
  10. Examples
  11. ========
  12. * Parsing (multipart) with default options:
  13. ```javascript
  14. var http = require('http'),
  15. inspect = require('util').inspect;
  16. var Busboy = require('busboy');
  17. http.createServer(function(req, res) {
  18. if (req.method === 'POST') {
  19. var busboy = new Busboy({ headers: req.headers });
  20. busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  21. console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
  22. file.on('data', function(data) {
  23. console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
  24. });
  25. file.on('end', function() {
  26. console.log('File [' + fieldname + '] Finished');
  27. });
  28. });
  29. busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
  30. console.log('Field [' + fieldname + ']: value: ' + inspect(val));
  31. });
  32. busboy.on('finish', function() {
  33. console.log('Done parsing form!');
  34. res.writeHead(303, { Connection: 'close', Location: '/' });
  35. res.end();
  36. });
  37. req.pipe(busboy);
  38. } else if (req.method === 'GET') {
  39. res.writeHead(200, { Connection: 'close' });
  40. res.end('<html><head></head><body>\
  41. <form method="POST" enctype="multipart/form-data">\
  42. <input type="text" name="textfield"><br />\
  43. <input type="file" name="filefield"><br />\
  44. <input type="submit">\
  45. </form>\
  46. </body></html>');
  47. }
  48. }).listen(8000, function() {
  49. console.log('Listening for requests');
  50. });
  51. // Example output, using http://nodejs.org/images/ryan-speaker.jpg as the file:
  52. //
  53. // Listening for requests
  54. // File [filefield]: filename: ryan-speaker.jpg, encoding: binary
  55. // File [filefield] got 11971 bytes
  56. // Field [textfield]: value: 'testing! :-)'
  57. // File [filefield] Finished
  58. // Done parsing form!
  59. ```
  60. * Save all incoming files to disk:
  61. ```javascript
  62. var http = require('http'),
  63. path = require('path'),
  64. os = require('os'),
  65. fs = require('fs');
  66. var Busboy = require('busboy');
  67. http.createServer(function(req, res) {
  68. if (req.method === 'POST') {
  69. var busboy = new Busboy({ headers: req.headers });
  70. busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  71. var saveTo = path.join(os.tmpDir(), path.basename(fieldname));
  72. file.pipe(fs.createWriteStream(saveTo));
  73. });
  74. busboy.on('finish', function() {
  75. res.writeHead(200, { 'Connection': 'close' });
  76. res.end("That's all folks!");
  77. });
  78. return req.pipe(busboy);
  79. }
  80. res.writeHead(404);
  81. res.end();
  82. }).listen(8000, function() {
  83. console.log('Listening for requests');
  84. });
  85. ```
  86. * Parsing (urlencoded) with default options:
  87. ```javascript
  88. var http = require('http'),
  89. inspect = require('util').inspect;
  90. var Busboy = require('busboy');
  91. http.createServer(function(req, res) {
  92. if (req.method === 'POST') {
  93. var busboy = new Busboy({ headers: req.headers });
  94. busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  95. console.log('File [' + fieldname + ']: filename: ' + filename);
  96. file.on('data', function(data) {
  97. console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
  98. });
  99. file.on('end', function() {
  100. console.log('File [' + fieldname + '] Finished');
  101. });
  102. });
  103. busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
  104. console.log('Field [' + fieldname + ']: value: ' + inspect(val));
  105. });
  106. busboy.on('finish', function() {
  107. console.log('Done parsing form!');
  108. res.writeHead(303, { Connection: 'close', Location: '/' });
  109. res.end();
  110. });
  111. req.pipe(busboy);
  112. } else if (req.method === 'GET') {
  113. res.writeHead(200, { Connection: 'close' });
  114. res.end('<html><head></head><body>\
  115. <form method="POST">\
  116. <input type="text" name="textfield"><br />\
  117. <select name="selectfield">\
  118. <option value="1">1</option>\
  119. <option value="10">10</option>\
  120. <option value="100">100</option>\
  121. <option value="9001">9001</option>\
  122. </select><br />\
  123. <input type="checkbox" name="checkfield">Node.js rules!<br />\
  124. <input type="submit">\
  125. </form>\
  126. </body></html>');
  127. }
  128. }).listen(8000, function() {
  129. console.log('Listening for requests');
  130. });
  131. // Example output:
  132. //
  133. // Listening for requests
  134. // Field [textfield]: value: 'testing! :-)'
  135. // Field [selectfield]: value: '9001'
  136. // Field [checkfield]: value: 'on'
  137. // Done parsing form!
  138. ```
  139. API
  140. ===
  141. _Busboy_ is a _Writable_ stream
  142. Busboy (special) events
  143. -----------------------
  144. * **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream.
  145. * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
  146. * If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
  147. * **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found.
  148. * **partsLimit**() - Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
  149. * **filesLimit**() - Emitted when specified `files` limit has been reached. No more 'file' events will be emitted.
  150. * **fieldsLimit**() - Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.
  151. Busboy methods
  152. --------------
  153. * **(constructor)**(< _object_ >config) - Creates and returns a new Busboy instance.
  154. * The constructor takes the following valid `config` settings:
  155. * **headers** - _object_ - These are the HTTP headers of the incoming request, which are used by individual parsers.
  156. * **highWaterMark** - _integer_ - highWaterMark to use for this Busboy instance (Default: WritableStream default).
  157. * **fileHwm** - _integer_ - highWaterMark to use for file streams (Default: ReadableStream default).
  158. * **defCharset** - _string_ - Default character set to use when one isn't defined (Default: 'utf8').
  159. * **preservePath** - _boolean_ - If paths in the multipart 'filename' field shall be preserved. (Default: false).
  160. * **limits** - _object_ - Various limits on incoming data. Valid properties are:
  161. * **fieldNameSize** - _integer_ - Max field name size (in bytes) (Default: 100 bytes).
  162. * **fieldSize** - _integer_ - Max field value size (in bytes) (Default: 1MB).
  163. * **fields** - _integer_ - Max number of non-file fields (Default: Infinity).
  164. * **fileSize** - _integer_ - For multipart forms, the max file size (in bytes) (Default: Infinity).
  165. * **files** - _integer_ - For multipart forms, the max number of file fields (Default: Infinity).
  166. * **parts** - _integer_ - For multipart forms, the max number of parts (fields + files) (Default: Infinity).
  167. * **headerPairs** - _integer_ - For multipart forms, the max number of header key=>value pairs to parse **Default:** 2000 (same as node's http).
  168. * The constructor can throw errors:
  169. * **Unsupported content type: $type** - The `Content-Type` isn't one Busboy can parse.
  170. * **Missing Content-Type** - The provided headers don't include `Content-Type` at all.