Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Post Form Data Node.js Express

I am getting form data in this form

'------WebKitFormBoundarysw7YYuBGKjAewMhe\r\nContent-Disposition: form-data; name': '"a"\r\n\r\nb\r\n------WebKitFormBoundarysw7YYuBGKjAewMhe--\r\n

I'm trying to find a middleware that will allow me to access the form data like:

req.body.a // -> 'b'

I've tried

var express = require('express');
var app = express();


var bodyParser = require('body-parser');

// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false }))

Is there a problem with my implementation or am I not using the correct middleware?

like image 354
Dan Baker Avatar asked Jun 04 '15 21:06

Dan Baker


1 Answers

The library which worked for me was express-formidable. Clean, fast and supports multipart requests too. Here is code from their docs

Install with:

npm install -S express-formidable

Here is sample usage:

const express = require('express');
const formidable = require('express-formidable');

var app = express();

app.use(formidable());

app.post('/upload', (req, res) => {
  req.fields; // contains non-file fields 
  req.files; // contains files 
});
like image 51
Chandan Purohit Avatar answered Oct 20 '22 08:10

Chandan Purohit