simple-multipart-formdata
v1.0.2
Published
To parase multipart form data body to a JSON object
Downloads
1
Maintainers
Readme
simple-multipart-formdata
Installation :
npm install simple-multipart-formdata
- Simple API to parse multipart-formdata into a readable object.
- Only requires HTTP request as an input which then return an event emitter. This will emit data event when form-data is ready. You need to assign an event listener to consume data which is coming as a readable object.
Note : It will not work with static types such as files. This works really well with all other types.
Example
const { formation } = require('simple-multipart-formdata');
let Form = formation( req );
Form.on( 'data' , (data)=> {
// do something
// data is readable object, containing parsed request body data
})
Explanation
formation( req ) get the HTTP request stream and return an event emitter which is fired when multipart-form-data has properly parsed.This is the main API of this module.
Form ( you can name this as you want ) is stored the return value which is an Event emitter
API
formation( [stream] )
return : emitter
Full Example :
HTTP Server which is handling incoming requests.
else if(req.method == "POST"){
let Info = {} ;
let Form = formation( req ); /* 'formation' do the body parsing and return a event emitter which emit -
when data is available */
Form.on('data' , (data)=> {
Info = data ;
console.log( data ); // to see how output looks like
});
req.on( 'end' , ()=> {
res.writeHead( 200 , { 'Content-Type': 'application/json'})
res.end( JSON.stringify(Info) ); // to see the parsed form-data
})
}
Here is the relavent html code.
<form action="http://localhost:8000" method="POST" enctype="multipart/form-data">
<input type="text" name="MYtext">
<input type="datetime-local" name="MYdatetime">
<input type="month" name="MYmonth">
<input type="time" name="Mytime">
<input type="search" name="MYsearch">
<input type="submit" name="SUBMITTED">
</form>
Here is the Output looks like.
{
MYtext: 'Gold Fish',
MYdatetime: {
date: '2021-10-01',
year: '2021',
month: '10',
day: '01',
time: '00:25',
hour: '00',
minute: '25'
},
MYmonth: { date: '2021-01', year: '2021', month: '01' },
Mytime: { time: '20:15', hour: '20', minute: '15' },
MYsearch: 'search for something',
SUBMITTED: 'Submit'
}
NICE ahh ............ :smiley::smiley:
following table shows the field(key) and data format of return object
| type | data format | |----------------------------------|----------------------| |checkbox , text , color , email | text/plain | |number , password , radio , range | text/plain | |search , tel , url | text/plain | |date |{ date,YY,MM,DD } | |datetime-local |{ datetime,YY,MM,DD,HR,MIN }| |month |{ date,YY,MM } | |week |{ date,YY,WW } | |time |{ time,HR,MIN } |
Here ;
YY -> year , MM -> month , WW -> week , DD -> day , HR -> hour , MIN -> minute