1 | var http = require('http'),
|
2 | inspect = require('util').inspect;
|
3 |
|
4 | var Busboy = require('busboy');
|
5 |
|
6 | http.createServer(function(req, res) {
|
7 | if (req.method === 'POST') {
|
8 |
|
9 | console.log(req.headers);
|
10 | var busboy = new Busboy({ headers: req.headers });
|
11 |
|
12 | busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
13 | console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding);
|
14 | file.on('data', function(data) {
|
15 | console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
|
16 | });
|
17 | file.on('end', function() {
|
18 | console.log('File [' + fieldname + '] Finished');
|
19 | });
|
20 | });
|
21 | busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
|
22 | console.log('Field [' + fieldname + ']: value: ' + inspect(val));
|
23 | });
|
24 | busboy.on('finish', function() {
|
25 | console.log('Done parsing form!');
|
26 | res.writeHead(303, { Connection: 'close', Location: '/' });
|
27 | res.end();
|
28 | });
|
29 |
|
30 | req.pipe(busboy);
|
31 |
|
32 | } else if (req.method === 'GET') {
|
33 | res.writeHead(200, { Connection: 'close' });
|
34 | res.end('<html><head></head><body>\
|
35 | <form method="POST" enctype="multipart/form-data">\
|
36 | <input type="text" name="textfield"><br />\
|
37 | <input type="file" name="filefield"><br />\
|
38 | <input type="submit">\
|
39 | </form>\
|
40 | </body></html>');
|
41 | }
|
42 | }).listen(3000, function() {
|
43 | console.log('Listening for requests');
|
44 | });
|