mean stack - Simple way to upload image with node.js and express? -



mean stack - Simple way to upload image with node.js and express? -

i've found few articles explaining process of them not date. how handle image upload in node.js?

i utilize busboy middleware in express parse out images in multipart/form-data request , works pretty nice.

my code looks like:

class="lang-js prettyprint-override">const busboy = require('connect-busboy'); //... app.use(busboy()); app.use(function parseuploadmw(req,res,next){ req.busboy.on('file', function onfile(fieldname, file, filename, encoding, mimetype) { file.fileread = []; file.on('data', function ondata(chunk) { this.fileread.push(chunk); }); file.on('error', function onerror(err) { console.log('error while buffering stream: ', err); //handle error }); file.on('end', function onend() { var finalbuffer = buffer.concat(this.fileread); req.files = req.files||{} req.files[fieldname] = { buffer: finalbuffer, size: finalbuffer.length, filename: filename, mimetype: mimetype.tolowercase() }; }); }); req.busboy.on('finish', function onfinish() { next() }); req.pipe(req.busboy); })

then files in req object @ req.files in express routes.

this technique works fine little images. if doing hardcore uploading, may want consider streaming files (to save memory) destination - s3 or similar - which can achieved busboy

another bundle popular , decent is: https://github.com/andrewrk/node-multiparty.

node.js mean-stack

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -