Handling file uploads with QuorraJS

QuorraJS
QuorraJS
Jul 23, 2017 · 2 min read

Uploading files is one of the most common tasks performed by web users. Users needs to use encryption type “multipart/form-data” to upload a file from browser. However the default QuorraJS(v1) input parser middleware (BodyParser) is only capable of parsing any of JSON, url-encoded, text and rawtypes of data. So here in this post I will explain how to create a custom multipart data parser middleware which can be used to handle multipart form data/file input from your QuorraJS application.

We will use multer, one of the popular NodeJS multipart input parser module in our custom middleware.

First cd to your QuorraJS project directory and install multer module by executing following npm command.

npm install multer --save

Now execute following Quorra cli command to generate a custom middleware scaffolding.

quorra generate-middleware MultipartParser

Now modify our custom middleware file app/middlewares/MultipartParser.js to require multer module and parse every incoming request with it.

I have passed option dest: ‘app/storage/uploads/’ to multer initialisation function so that upload files will be saved to to app/storage/uploads directory.

Our custom middleware is now ready to use. Now register this custom middleware with Quorra application(docs: registering custom middleware).

And then you are done! Just post the HTML multipart form to your QuorraJS application and you can access uploaded files from req.files object and all other field data by calling req.input.all method.

QuorraJS

Written by

QuorraJS

NodeJS MVC Framework inspired by @Laravel | https://quorrajs.org | Currently in Beta

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade