Node.js - Formidable for multipart/form-data upload is not working

2,172 views
Skip to first unread message

nk

unread,
Nov 12, 2019, 2:13:02 PM11/12/19
to nodejs
  const express = require('express');
    const app = express();    
    const formidable = require('formidable');

    app.post('/upload', (req, res) => {
      let form = new formidable.IncomingForm();
      form.parse(req, (err, fields, files) => {
         if(err) {
            console.log(err);
            return res.send(err);
         } else {
            console.log(fields);
            console.log(files);
            return res.send(fields);
         }
    });
    app.listen(8080, () => console.log('App is running');

The following code throws the error after a long time. The error is -

 Error: Request aborted

Request is sent through Postman with form-data Body, fields are description(text) and input(file)

Lukas Wilkeer

unread,
Nov 14, 2019, 11:33:21 AM11/14/19
to nodejs
Adding the express-body-parser should work (if is not a problem with formidable)
Said that, i think that the data that has been send through HTTP has not been parsed.

const bodyParser = require('body-paraer')

app.use('bodyParser', { urlencoded: true }) // for the xxx-url-form-urlencoded.

Printing the entire req.body is a great way to ensure that, if you attempt that probability i'm wrong. :)

Reply all
Reply to author
Forward
0 new messages