Created
April 22, 2015 06:21
-
-
Save diablowu/f6947d024d0248dd2afa to your computer and use it in GitHub Desktop.
fileupload server over Busboy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'), | |
| inspect = require('util').inspect; | |
| var Busboy = require('busboy'); | |
| http.createServer(function(req, res) { | |
| if (req.method === 'POST') { | |
| var busboy = new Busboy({ headers: req.headers }); | |
| busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
| console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); | |
| file.on('data', function(data) { | |
| console.log('File [' + fieldname + '] got ' + data.length + ' bytes'); | |
| }); | |
| file.on('end', function() { | |
| console.log('File [' + fieldname + '] Finished'); | |
| }); | |
| }); | |
| busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) { | |
| console.log('Field [' + fieldname + ']: value: ' + inspect(val)); | |
| }); | |
| busboy.on('finish', function() { | |
| console.log('Done parsing form!'); | |
| res.writeHead(303, { Connection: 'close', Location: '/' }); | |
| res.end(); | |
| }); | |
| req.pipe(busboy); | |
| } else if (req.method === 'GET') { | |
| res.writeHead(200, { Connection: 'close' }); | |
| res.end('<html><head></head><body>\ | |
| <form method="POST" enctype="multipart/form-data">\ | |
| <input type="text" name="textfield"><br />\ | |
| <input type="file" name="filefield"><br />\ | |
| <input type="submit">\ | |
| </form>\ | |
| </body></html>'); | |
| } | |
| }).listen(8111, function() { | |
| console.log('Listening for requests'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment