What is dicer?
The dicer npm package is a very fast streaming multipart parser that can be used to parse multipart/form-data input. It is particularly useful for handling file uploads and multipart HTTP requests in Node.js applications.
What are dicer's main functionalities?
Multipart Parsing
This code sets up a new Dicer instance with a specified boundary and listens for 'part' events to process each part of the multipart data. It also listens for the 'finish' event to know when all parts have been processed.
const Dicer = require('dicer');
const d = new Dicer({ boundary: '---------------------------168072824752491622650073' });
d.on('part', function(p) {
console.log('New part!');
p.on('data', function(data) {
console.log('Part data: ' + data.toString());
});
p.on('end', function() {
console.log('End of part');
});
});
d.on('finish', function() {
console.log('End of parts');
});
d.write('some multipart data buffer');
Other packages similar to dicer
busboy
Busboy is a Node.js module for parsing incoming HTML form data. It is similar to dicer in that it can handle multipart/form-data, but it also supports other form data types and is often used for handling file uploads in web applications.
multiparty
Multiparty is another multipart/form-data parser for Node.js. It is used for parsing form uploads and is similar to dicer. However, multiparty provides a higher-level API and can be easier to use for some developers compared to the lower-level stream handling required by dicer.
formidable
Formidable is a Node.js module for parsing form data, especially file uploads. It compares to dicer in its ability to handle multipart/form-data but also offers additional features such as file upload progress events and support for multiple file uploads.
Description
A very fast streaming multipart parser for node.js.
Requirements
Install
npm install dicer
Examples
- Parse an HTTP form upload
var inspect = require('util').inspect,
http = require('http');
var Dicer = require('dicer');
var RE_BOUNDARY = /^multipart\/form-data.*?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i;
http.createServer(function(req, res) {
var m;
if (req.header['content-type'] && (m = RE_BOUNDARY.exec(req.header['content-type']))) {
var d = new Dicer({ boundary: m[1] || m[2] });
d.on('part', function(p) {
console.log('New part!');
p.on('header', function(header) {
for (var h in header)
console.log('Part header: k: ' + inspect(h) + ', v: ' + inspect(header[h]));
});
p.on('data', function(data) {
console.log('Part data: ' + inspect(data.toString()));
});
p.on('end', function() {
console.log('End of part\n');
});
});
d.on('end', function() {
console.log('End of parts');
res.writeHead(200);
res.end();
});
req.pipe(d);
} else {
res.writeHead(404);
res.end();
}
}).listen(8080, function() {
console.log('Listening for requests');
});
API
Dicer is a WritableStream
Dicer (special) events
-
end() - Emitted when all parts have been parsed.
-
part(< PartStream >stream) - Emitted when a new part has been found.
-
preamble(< ReadableStream >stream) - Emitted for preamble data if you should happen to need it (this should almost always be ignored though).
-
trailer(< Buffer >data) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this should be ignored too).
Dicer methods
PartStream is a ReadableStream
PartStream (special) events
- header(< object >header) - An object containing the header for this particular part. Each property value is an array of one or more header values.