@coolgk/formdata
a javascript / typescript module
npm install @coolgk/formdata
A http request form data parser (large file friendly) for 'application/json', 'application/x-www-form-urlencoded' and 'multipart/form-data'. It only parses form data when you ask for it.
Report bugs here: https://github.com/coolgk/node-utils/issues
Example Form
<form method="POST" enctype="multipart/form-data">
<input type="text" name="name">
<input type="text" name="age">
<input type="file" name="photo">
<input type="file" name="photo">
<input type="file" name="id">
</form>
Express Middleware
const app = require('express')();
const formdata = require('@coolgk/formdata');
app.use(formdata.express());
app.post('/id-only', async (request, response, next) => {
const post = await request.formdata.getData('id');
console.log(post);
response.json(post);
});
app.post('/all-files', async (request, response, next) => {
const post = await request.formdata.getData(['id', 'photo']);
console.log(post);
response.json(post);
});
app.listen(8888);
Native Node App
const { formData, express, getFormData, FormDataError } = require('@coolgk/formdata');
const http = require('http');
http.createServer(async (request, response) => {
const data = await getFormData(request, { fileFieldNames: ['id', 'photo'] });
console.log(data);
response.end(JSON.stringify(data));
}).listen(8888);