Handling file uploads with QuorraJS

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 --saveNow execute following Quorra cli command to generate a custom middleware scaffolding.
quorra generate-middleware MultipartParserNow 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.
