root / HServer / 00.Server / 00.Program / node_modules / dicer / README.md
이력 | 보기 | 이력해설 | 다운로드 (3.57 KB)
| 1 |
|
|---|---|
| 2 |
Description |
| 3 |
=========== |
| 4 |
|
| 5 |
A very fast streaming multipart parser for node.js. |
| 6 |
|
| 7 |
Benchmarks can be found [here](https://github.com/mscdex/dicer/wiki/Benchmarks). |
| 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 dicer |
| 20 |
|
| 21 |
|
| 22 |
Examples |
| 23 |
======== |
| 24 |
|
| 25 |
* Parse an HTTP form upload |
| 26 |
|
| 27 |
```javascript |
| 28 |
var inspect = require('util').inspect,
|
| 29 |
http = require('http');
|
| 30 |
|
| 31 |
var Dicer = require('dicer');
|
| 32 |
|
| 33 |
// quick and dirty way to parse multipart boundary |
| 34 |
var RE_BOUNDARY = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i, |
| 35 |
HTML = new Buffer('<html><head></head><body>\
|
| 36 |
<form method="POST" enctype="multipart/form-data">\ |
| 37 |
<input type="text" name="textfield"><br />\ |
| 38 |
<input type="file" name="filefield"><br />\ |
| 39 |
<input type="submit">\ |
| 40 |
</form>\ |
| 41 |
</body></html>'), |
| 42 |
PORT = 8080; |
| 43 |
|
| 44 |
http.createServer(function(req, res) {
|
| 45 |
var m; |
| 46 |
if (req.method === 'POST' |
| 47 |
&& req.headers['content-type'] |
| 48 |
&& (m = RE_BOUNDARY.exec(req.headers['content-type']))) {
|
| 49 |
var d = new Dicer({ boundary: m[1] || m[2] });
|
| 50 |
|
| 51 |
d.on('part', function(p) {
|
| 52 |
console.log('New part!');
|
| 53 |
p.on('header', function(header) {
|
| 54 |
for (var h in header) {
|
| 55 |
console.log('Part header: k: ' + inspect(h)
|
| 56 |
+ ', v: ' + inspect(header[h])); |
| 57 |
} |
| 58 |
}); |
| 59 |
p.on('data', function(data) {
|
| 60 |
console.log('Part data: ' + inspect(data.toString()));
|
| 61 |
}); |
| 62 |
p.on('end', function() {
|
| 63 |
console.log('End of part\n');
|
| 64 |
}); |
| 65 |
}); |
| 66 |
d.on('finish', function() {
|
| 67 |
console.log('End of parts');
|
| 68 |
res.writeHead(200); |
| 69 |
res.end('Form submission successful!');
|
| 70 |
}); |
| 71 |
req.pipe(d); |
| 72 |
} else if (req.method === 'GET' && req.url === '/') {
|
| 73 |
res.writeHead(200); |
| 74 |
res.end(HTML); |
| 75 |
} else {
|
| 76 |
res.writeHead(404); |
| 77 |
res.end(); |
| 78 |
} |
| 79 |
}).listen(PORT, function() {
|
| 80 |
console.log('Listening for requests on port ' + PORT);
|
| 81 |
}); |
| 82 |
``` |
| 83 |
|
| 84 |
|
| 85 |
API |
| 86 |
=== |
| 87 |
|
| 88 |
_Dicer_ is a _WritableStream_ |
| 89 |
|
| 90 |
Dicer (special) events |
| 91 |
---------------------- |
| 92 |
|
| 93 |
* **finish**() - Emitted when all parts have been parsed and the Dicer instance has been ended. |
| 94 |
|
| 95 |
* **part**(< _PartStream_ >stream) - Emitted when a new part has been found. |
| 96 |
|
| 97 |
* **preamble**(< _PartStream_ >stream) - Emitted for preamble if you should happen to need it (can usually be ignored). |
| 98 |
|
| 99 |
* **trailer**(< _Buffer_ >data) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this can usually be ignored too). |
| 100 |
|
| 101 |
|
| 102 |
Dicer methods |
| 103 |
------------- |
| 104 |
|
| 105 |
* **(constructor)**(< _object_ >config) - Creates and returns a new Dicer instance with the following valid `config` settings: |
| 106 |
|
| 107 |
* **boundary** - _string_ - This is the boundary used to detect the beginning of a new part. |
| 108 |
|
| 109 |
* **headerFirst** - _boolean_ - If true, preamble header parsing will be performed first. |
| 110 |
|
| 111 |
* **maxHeaderPairs** - _integer_ - The maximum number of header key=>value pairs to parse **Default:** 2000 (same as node's http). |
| 112 |
|
| 113 |
* **setBoundary**(< _string_ >boundary) - _(void)_ - Sets the boundary to use for parsing and performs some initialization needed for parsing. You should only need to use this if you set `headerFirst` to true in the constructor and are parsing the boundary from the preamble header. |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
_PartStream_ is a _ReadableStream_ |
| 118 |
|
| 119 |
PartStream (special) events |
| 120 |
--------------------------- |
| 121 |
|
| 122 |
* **header**(< _object_ >header) - An object containing the header for this particular part. Each property value is an _array_ of one or more string values. |