Socket
Socket
Sign inDemoInstall

@coolgk/formdata

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

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.


Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

@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
// 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'); // upload 3 files but only parse 1, ignore others
    console.log(post);
    response.json(post);
    // output
    // {
        // "name": "Tim",
        // "age": "33",
        // "id": {
            // "error": null,
            // "fieldname": "id",
            // "filename": "test.txt",
            // "encoding": "7bit",
            // "mimetype": "text/plain",
            // "size": 13,
            // "path": "/tmp/151605931497716067xZGgxPUdNvoj"
        // }
    // }
});

app.post('/all-files', async (request, response, next) => {
    const post = await request.formdata.getData(['id', 'photo']); // parse all files
    console.log(post);
    response.json(post);
    // output
    // {
        // "name": "Tim",
        // "age": "33",
        // "photo": [
            // {
                // "error": null,
                // "fieldname": "photo",
                // "filename": "test.png",
                // "encoding": "7bit",
                // "mimetype": "image/png",
                // "size": 604,
                // "path": "/tmp/151605931497716067xZGgxPUdNvoj"
            // },
            // {
                // "error": null,
                // "fieldname": "photo",
                // "filename": "test.svg",
                // "encoding": "7bit",
                // "mimetype": "image/svg+xml",
                // "size": 2484,
                // "path": "/tmp/151605931497916067EAUAa3yB4q42"
            // }
        // ],
        // "id": {
            // "error": null,
            // "fieldname": "id",
            // "filename": "test.txt",
            // "encoding": "7bit",
            // "mimetype": "text/plain",
            // "size": 13,
            // "path": "/tmp/151605931498016067zqZe6dlhidQ5"
        // }
    // }
});

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'] });

    // OR
    // const formdata = formData(request);
    // ... some middelware
    // ... in some routes
    // const data = formdata.getData(['id', 'photo']);

    console.log(data);
    response.end(JSON.stringify(data));

    // {
        // "name": "Tim",
        // "age": "33",
        // "photo": [
            // {
                // "error": null,
                // "fieldname": "photo",
                // "filename": "test.png",
                // "encoding": "7bit",
                // "mimetype": "image/png",
                // "size": 604,
                // "path": "/tmp/151605931497716067xZGgxPUdNvoj"
            // },
            // {
                // "error": null,
                // "fieldname": "photo",
                // "filename": "test.svg",
                // "encoding": "7bit",
                // "mimetype": "image/svg+xml",
                // "size": 2484,
                // "path": "/tmp/151605931497916067EAUAa3yB4q42"
            // }
        // ],
        // "id": {
            // "error": null,
            // "fieldname": "id",
            // "filename": "test.txt",
            // "encoding": "7bit",
            // "mimetype": "text/plain",
            // "size": 13,
            // "path": "/tmp/151605931498016067zqZe6dlhidQ5"
        // }
    // }

}).listen(8888);

Keywords

FAQs

Package last updated on 08 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc