root / HServer / 00.Server / 00.Program / node_modules / busboy / README.md
이력 | 보기 | 이력해설 | 다운로드 (8.6 KB)
| 1 | 39 | HKM | Description |
|---|---|---|---|
| 2 | =========== |
||
| 3 | |||
| 4 | A node.js module for parsing incoming HTML form data. |
||
| 5 | |||
| 6 | If you've found this module to be useful and wish to support it, you may do so by visiting this pledgie campaign: |
||
| 7 | <a href='https://pledgie.com/campaigns/28774'><img alt='Click here to support busboy' src='https://pledgie.com/campaigns/28774.png?skin_name=chrome' border='0'></a> |
||
| 8 | |||
| 9 | |||
| 10 | Requirements |
||
| 11 | ============ |
||
| 12 | |||
| 13 | * [node.js](http://nodejs.org/) -- v0.8.0 or newer |
||
| 14 | |||
| 15 | |||
| 16 | Install |
||
| 17 | ======= |
||
| 18 | |||
| 19 | npm install busboy |
||
| 20 | |||
| 21 | |||
| 22 | Examples |
||
| 23 | ======== |
||
| 24 | |||
| 25 | * Parsing (multipart) with default options: |
||
| 26 | |||
| 27 | ```javascript |
||
| 28 | var http = require('http'),
|
||
| 29 | inspect = require('util').inspect;
|
||
| 30 | |||
| 31 | var Busboy = require('busboy');
|
||
| 32 | |||
| 33 | http.createServer(function(req, res) {
|
||
| 34 | if (req.method === 'POST') {
|
||
| 35 | var busboy = new Busboy({ headers: req.headers });
|
||
| 36 | busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
||
| 37 | console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
|
||
| 38 | file.on('data', function(data) {
|
||
| 39 | console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
|
||
| 40 | }); |
||
| 41 | file.on('end', function() {
|
||
| 42 | console.log('File [' + fieldname + '] Finished');
|
||
| 43 | }); |
||
| 44 | }); |
||
| 45 | busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
|
||
| 46 | console.log('Field [' + fieldname + ']: value: ' + inspect(val));
|
||
| 47 | }); |
||
| 48 | busboy.on('finish', function() {
|
||
| 49 | console.log('Done parsing form!');
|
||
| 50 | res.writeHead(303, { Connection: 'close', Location: '/' });
|
||
| 51 | res.end(); |
||
| 52 | }); |
||
| 53 | req.pipe(busboy); |
||
| 54 | } else if (req.method === 'GET') {
|
||
| 55 | res.writeHead(200, { Connection: 'close' });
|
||
| 56 | res.end('<html><head></head><body>\
|
||
| 57 | <form method="POST" enctype="multipart/form-data">\ |
||
| 58 | <input type="text" name="textfield"><br />\ |
||
| 59 | <input type="file" name="filefield"><br />\ |
||
| 60 | <input type="submit">\ |
||
| 61 | </form>\ |
||
| 62 | </body></html>'); |
||
| 63 | } |
||
| 64 | }).listen(8000, function() {
|
||
| 65 | console.log('Listening for requests');
|
||
| 66 | }); |
||
| 67 | |||
| 68 | // Example output, using http://nodejs.org/images/ryan-speaker.jpg as the file: |
||
| 69 | // |
||
| 70 | // Listening for requests |
||
| 71 | // File [filefield]: filename: ryan-speaker.jpg, encoding: binary |
||
| 72 | // File [filefield] got 11971 bytes |
||
| 73 | // Field [textfield]: value: 'testing! :-)' |
||
| 74 | // File [filefield] Finished |
||
| 75 | // Done parsing form! |
||
| 76 | ``` |
||
| 77 | |||
| 78 | * Save all incoming files to disk: |
||
| 79 | |||
| 80 | ```javascript |
||
| 81 | var http = require('http'),
|
||
| 82 | path = require('path'),
|
||
| 83 | os = require('os'),
|
||
| 84 | fs = require('fs');
|
||
| 85 | |||
| 86 | var Busboy = require('busboy');
|
||
| 87 | |||
| 88 | http.createServer(function(req, res) {
|
||
| 89 | if (req.method === 'POST') {
|
||
| 90 | var busboy = new Busboy({ headers: req.headers });
|
||
| 91 | busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
||
| 92 | var saveTo = path.join(os.tmpDir(), path.basename(fieldname)); |
||
| 93 | file.pipe(fs.createWriteStream(saveTo)); |
||
| 94 | }); |
||
| 95 | busboy.on('finish', function() {
|
||
| 96 | res.writeHead(200, { 'Connection': 'close' });
|
||
| 97 | res.end("That's all folks!");
|
||
| 98 | }); |
||
| 99 | return req.pipe(busboy); |
||
| 100 | } |
||
| 101 | res.writeHead(404); |
||
| 102 | res.end(); |
||
| 103 | }).listen(8000, function() {
|
||
| 104 | console.log('Listening for requests');
|
||
| 105 | }); |
||
| 106 | ``` |
||
| 107 | |||
| 108 | * Parsing (urlencoded) with default options: |
||
| 109 | |||
| 110 | ```javascript |
||
| 111 | var http = require('http'),
|
||
| 112 | inspect = require('util').inspect;
|
||
| 113 | |||
| 114 | var Busboy = require('busboy');
|
||
| 115 | |||
| 116 | http.createServer(function(req, res) {
|
||
| 117 | if (req.method === 'POST') {
|
||
| 118 | var busboy = new Busboy({ headers: req.headers });
|
||
| 119 | busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
||
| 120 | console.log('File [' + fieldname + ']: filename: ' + filename);
|
||
| 121 | file.on('data', function(data) {
|
||
| 122 | console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
|
||
| 123 | }); |
||
| 124 | file.on('end', function() {
|
||
| 125 | console.log('File [' + fieldname + '] Finished');
|
||
| 126 | }); |
||
| 127 | }); |
||
| 128 | busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
|
||
| 129 | console.log('Field [' + fieldname + ']: value: ' + inspect(val));
|
||
| 130 | }); |
||
| 131 | busboy.on('finish', function() {
|
||
| 132 | console.log('Done parsing form!');
|
||
| 133 | res.writeHead(303, { Connection: 'close', Location: '/' });
|
||
| 134 | res.end(); |
||
| 135 | }); |
||
| 136 | req.pipe(busboy); |
||
| 137 | } else if (req.method === 'GET') {
|
||
| 138 | res.writeHead(200, { Connection: 'close' });
|
||
| 139 | res.end('<html><head></head><body>\
|
||
| 140 | <form method="POST">\ |
||
| 141 | <input type="text" name="textfield"><br />\ |
||
| 142 | <select name="selectfield">\ |
||
| 143 | <option value="1">1</option>\ |
||
| 144 | <option value="10">10</option>\ |
||
| 145 | <option value="100">100</option>\ |
||
| 146 | <option value="9001">9001</option>\ |
||
| 147 | </select><br />\ |
||
| 148 | <input type="checkbox" name="checkfield">Node.js rules!<br />\ |
||
| 149 | <input type="submit">\ |
||
| 150 | </form>\ |
||
| 151 | </body></html>'); |
||
| 152 | } |
||
| 153 | }).listen(8000, function() {
|
||
| 154 | console.log('Listening for requests');
|
||
| 155 | }); |
||
| 156 | |||
| 157 | // Example output: |
||
| 158 | // |
||
| 159 | // Listening for requests |
||
| 160 | // Field [textfield]: value: 'testing! :-)' |
||
| 161 | // Field [selectfield]: value: '9001' |
||
| 162 | // Field [checkfield]: value: 'on' |
||
| 163 | // Done parsing form! |
||
| 164 | ``` |
||
| 165 | |||
| 166 | |||
| 167 | API |
||
| 168 | === |
||
| 169 | |||
| 170 | _Busboy_ is a _Writable_ stream |
||
| 171 | |||
| 172 | Busboy (special) events |
||
| 173 | ----------------------- |
||
| 174 | |||
| 175 | * **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. |
||
| 176 | * 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). |
||
| 177 | * 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. |
||
| 178 | |||
| 179 | * **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found. |
||
| 180 | |||
| 181 | * **partsLimit**() - Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted. |
||
| 182 | |||
| 183 | * **filesLimit**() - Emitted when specified `files` limit has been reached. No more 'file' events will be emitted. |
||
| 184 | |||
| 185 | * **fieldsLimit**() - Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted. |
||
| 186 | |||
| 187 | |||
| 188 | Busboy methods |
||
| 189 | -------------- |
||
| 190 | |||
| 191 | * **(constructor)**(< _object_ >config) - Creates and returns a new Busboy instance. |
||
| 192 | |||
| 193 | * The constructor takes the following valid `config` settings: |
||
| 194 | |||
| 195 | * **headers** - _object_ - These are the HTTP headers of the incoming request, which are used by individual parsers. |
||
| 196 | |||
| 197 | * **highWaterMark** - _integer_ - highWaterMark to use for this Busboy instance (Default: WritableStream default). |
||
| 198 | |||
| 199 | * **fileHwm** - _integer_ - highWaterMark to use for file streams (Default: ReadableStream default). |
||
| 200 | |||
| 201 | * **defCharset** - _string_ - Default character set to use when one isn't defined (Default: 'utf8'). |
||
| 202 | |||
| 203 | * **preservePath** - _boolean_ - If paths in the multipart 'filename' field shall be preserved. (Default: false). |
||
| 204 | |||
| 205 | * **limits** - _object_ - Various limits on incoming data. Valid properties are: |
||
| 206 | |||
| 207 | * **fieldNameSize** - _integer_ - Max field name size (in bytes) (Default: 100 bytes). |
||
| 208 | |||
| 209 | * **fieldSize** - _integer_ - Max field value size (in bytes) (Default: 1MB). |
||
| 210 | |||
| 211 | * **fields** - _integer_ - Max number of non-file fields (Default: Infinity). |
||
| 212 | |||
| 213 | * **fileSize** - _integer_ - For multipart forms, the max file size (in bytes) (Default: Infinity). |
||
| 214 | |||
| 215 | * **files** - _integer_ - For multipart forms, the max number of file fields (Default: Infinity). |
||
| 216 | |||
| 217 | * **parts** - _integer_ - For multipart forms, the max number of parts (fields + files) (Default: Infinity). |
||
| 218 | |||
| 219 | * **headerPairs** - _integer_ - For multipart forms, the max number of header key=>value pairs to parse **Default:** 2000 (same as node's http). |
||
| 220 | |||
| 221 | * The constructor can throw errors: |
||
| 222 | |||
| 223 | * **Unsupported content type: $type** - The `Content-Type` isn't one Busboy can parse. |
||
| 224 | |||
| 225 | * **Missing Content-Type** - The provided headers don't include `Content-Type` at all. |