Skip to content

Commit 26b7640

Browse files
committed
fix: add docs example
Signed-off-by: Charlike Mike Reagent <[email protected]>
1 parent 8231ea6 commit 26b7640

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ form.bytesExpected;
183183

184184
### .parse(request, callback)
185185

186-
Parses an incoming Node.js `request` containing form data. If `callback` is provided, all fields and files are collected and passed to the callback:
186+
Parses an incoming Node.js `request` containing form data.
187+
If `callback` is provided, all fields and files are collected and passed to the callback.
187188

188189
```js
189190
const formidable = require('formidable');
@@ -198,6 +199,58 @@ form.parse(req, (err, fields, files) => {
198199

199200
You may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any `'field'` / `'file'` events processing which would occur otherwise, making you fully responsible for handling the processing.
200201

202+
In the example below, we listen on couple of events and direct them to the `data` listener,
203+
so you can do whatever you choose there, based on whether its before the file been emitted,
204+
the header value, the header name, on field, on file and etc.
205+
206+
Or the other way could be to just override the `form.onPart` as it's shown a bit later.
207+
208+
```js
209+
form.once('error', console.error);
210+
211+
form.on('fileBegin', (filename, file) => {
212+
form.emit('data', { name: 'fileBegin', filename, value: file });
213+
});
214+
215+
form.on('file', (filename, file) => {
216+
form.emit('data', { name: 'file', key: filename, value: file });
217+
});
218+
219+
form.on('field', (fieldName, fieldValue) => {
220+
form.emit('data', { name: 'field', key: fieldName, value: fieldValue });
221+
});
222+
223+
form.once('end', () => {
224+
console.log('Done!');
225+
});
226+
227+
// If you want to customize whatever you want...
228+
form.on('data', ({ name, key, value, buffer, start, end, ...more }) => {
229+
if (name === 'partBegin') {
230+
}
231+
if (name === 'partData') {
232+
}
233+
if (name === 'headerField') {
234+
}
235+
if (name === 'headerValue') {
236+
}
237+
if (name === 'headerEnd') {
238+
}
239+
if (name === 'headersEnd') {
240+
}
241+
if (name === 'field') {
242+
console.log('field name:', key);
243+
console.log('field value:', value);
244+
}
245+
if (name === 'file') {
246+
console.log('file:', key, value);
247+
}
248+
if (name === 'fileBegin') {
249+
console.log('fileBegin:', key, value);
250+
}
251+
});
252+
```
253+
201254
### form.onPart
202255

203256
If you want to use Formidable to only handle certain parts for you, you can do something similar.
@@ -251,7 +304,7 @@ export interface File {
251304
// Mostly here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
252305
file.lastModifiedDate: Date | null;
253306

254-
// If hash calculation was set, you can read the hex digest out of this var.
307+
// If `options.hash` calculation was set, you can read the hex digest out of this var.
255308
file.hash: string | 'sha1' | 'md5' | 'sha256' | null;
256309
}
257310
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "formidable",
3-
"version": "2.0.0-canary.20200129.2",
3+
"version": "2.0.0-canary.20200129.3",
44
"license": "MIT",
55
"description": "A node.js module for parsing form data, especially file uploads.",
66
"homepage": "https://github.com/node-formidable/node-formidable",

test/unit/test-querystring-parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ test(function write() {
2626
assert.equal(parser.bufferLength, a.length + b.length);
2727
});
2828

29+
// ! skip
2930
// test(function end() {
3031
// const FIELDS = { a: ['b', { c: 'd' }], e: 'f' };
3132

0 commit comments

Comments
 (0)