node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.01k stars 680 forks source link

app.use(express.json()) breaks Formidable #748

Closed szarkowicz closed 2 years ago

szarkowicz commented 3 years ago

Context

What are you trying to achieve or the steps to reproduce?

const express = require('express');
const app = express();
app.use(express.json()); // <-- breaks formidable

let formidable = require('formidable');

let router = express.Router();

router.route('/v1/user-form')
.post(function (req, res)
{
    try
    {
        let form = new formidable.IncomingForm();

        form.parse(req, function(err, fields, files)
        {
           console.log('form fields', fields);
        });
    }
    catch(error)  
    {
        console.log(error);
    }
});

When I add app.use(express.json()); to my server.js file this line causes formidable to break / hang with no error. As soon as I comment out that line, formidable works as expected.

I know that app.use(express.json()) gives access to req.body but not sure why this is breaking formidable.

When I debug the formidable.js file I noticed that on('data') is never called when express.json() is used.

    // Start listening for data.
    req
      .on('error', (err) => {
        this._error(err);
      })
      .on('aborted', () => {
        this.emit('aborted');
        this._error(new Error('Request aborted'));
      })
      .on('data', (buffer) => {
        try {
          this.write(buffer);
        } catch (err) {
          this._error(err);
        }
      })
      .on('end', () => {
        if (this.error) {
          return;
        }
        if (this._parser) {
          this._parser.end();
        }
        this._maybeEnd();
      });

I googled a bunch of times to see if there was any similar issues but I could not find anyone that was was having similar issue.

Thanks in advance for any help or suggestions.

szarkowicz commented 3 years ago

Doing some more digging - this only breaks / hangs when I am sending JSON data - example sending JSON through POSTMAN. Mutlipart form still works

GrosSacASac commented 3 years ago

what happens if you remove app.use(express.json()); // <-- breaks formidable this line, formidable should already handle json

szarkowicz commented 3 years ago

Hi GrosSacASac,

That is correct that it does, but I need to be be able to pass req and have req.body populated to our logging API. So when I enable app.use(express.json()); - I can just pass the request as middleware. Once I turn this on Formidable no longer works with submitting JSON data.

GrosSacASac commented 2 years ago

use formidable as a middleware, in the middleware use req.body = fields

tunnckoCore commented 2 years ago

We are handling json by deafult too. Disable the json plugin (see "enabledPlugins" docs) and probably the urlencodeded one, and then you should be able to use express.json() without problems.

for us: @GrosSacASac we probably should export a proper middleware which would be recommended. i always thought for that.

GrosSacASac commented 2 years ago

added example https://github.com/node-formidable/formidable/commit/7323c267edffe84c39d8711a61284086e780d455

leksyking commented 1 year ago

Use this package: express-formidable-v2 Check this []https://github.com/Abderrahman-byte/express-formidable-v2 out, It is a fork of "express-formidable" package

Now you have req.fields and req.body coexisting