How to get POST fields in Express without using bodyParser middleware?

In recent versions of Express, the recommendation (passed through a debug message) is to stop using the bodyParser . I read a little, and it looks like bodyParser is a wrapper for json and urlencoded middlewares environments - and now, the most recent version of Express (3.4.4) uses these 2 instead of bodyParser out boxes - great, right?

But now I can’t get to my fields. req.body - undefined. Here is my JS form submission code (text fields only, no files). Can someone please tell me which req property / function to use to get the values?

 var formData = new FormData($('#myForm')[0]); $.ajax({ url: '/myurl', cache: false, contentType: false, processData: false, data: formData, type: 'POST', success: function(data) { console.log(data); }, error: function(jqXHR, textStatus, errorThrown) { console.error('Error occured: ' + errorThrown); } }); 
+6
source share
1 answer

The problem is that when submitting FormData Content-Type will be multipart/form-data .

Although you use express.json() and express.urlencoded() , each of them only affects Content-Type - application/json and application/x-www-form-urlencoded respectively.

And Express / Connect will remove the built-in multipart() support and parsing the contents of multipart/form-data in the future due to security issues. Instead, they recommend using :

So, for future support for FormData and multi-part in general with Express / Connect, you will have to use the add dependency.

+13
source

Source: https://habr.com/ru/post/958887/


All Articles