question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Context

  • node version: 12.6.0
  • module (formidable) version: canary
  • environment (e.g. node, browser, native, OS): node

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tunnckoCorecommented, Jul 16, 2021

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.

0reactions
leksykingcommented, Aug 25, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express.js POST Requests not working after installing express ...
Okay I found a way to send requests through the multipart/form data and the application/json without breaking any thing.
Read more >
5.x API - Express.js
Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches ... Routing HTTP requests; see for example,...
Read more >
What Does `app.use(express.json())` Do in Express?
express.json() is a built in middleware function in Express starting from v4.16.0. It parses incoming JSON requests and puts the parsed data in ......
Read more >
body-parser - npm
Express /Connect top-level generic. This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will ...
Read more >
bodyParser is deprecated express 4 - node.js
body-parser deprecated bodyParser: use individual json/urlencoded ... app.use(express.urlencoded({extended: true})); app.use(express.json()) // To parse the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found