npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

multi-part-form-data-upload

v1.0.113

Published

MultiPartFormData is a simple all in one multipart/form-data parser for Node.js

Downloads

10

Readme

Multi Part Form Data Upload

A full fledge node js module for parsing the multipart/form data.

Features

  • Parse Multiple Files, and uploads them to desired location
  • Parse Non File fields
  • No need for extra plugin to parse text fields
  • Parse & Return all fields exactly as they are on browser from
  • Fast and can work with any framework

Installation

Install my-project with npm

  npm install multi-part-form-data-upload

Usage

// Express
const uploader = require('multi-part-form-data-upload')(options /* config options */ );
const app = express();

app.post('/uploads',uploader, (req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ body: req.body }));
});

//  OR Http

const http = require('http');
const uploader = require('multi-part-form-data-upload')(options /* config options */ );

const server = http.createServer(async (req, res) => {
  if (req.url === '/uploads' && req.method.toLowerCase() === 'post') {
    await uploader(req, res, () => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ body: req.body }));
    });
    return;
  }
}

Usage/Examples

Node.js Http Module

const moduleName = 'http';
const http = require('http');
const uploader = require('multi-part-form-data-upload');

const server = http.createServer(async (req, res) => {
  if (req.url === '/uploads' && req.method.toLowerCase() === 'post') {
    await uploader(/* configs */)(req, res, () => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ body: req.body }));
    });
    return;
  }

  // Upload Form HTML
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end(`
  <h1>MultiPartFormData with Node.js <code>"${moduleName}"</code> module</h1>

  <form action="/uploads" enctype="multipart/form-data" method="post">
    <div>Text field title: <input type="text" name="title" /></div>
    <div>MultipleFiles: <input type="file" name="multipleFiles" multiple="multiple" /></div>
    <div>File1: <input type="file" name="file1" /></div>
    <div>File2: <input type="file" name="file2"  /></div>
    <input type="submit" value="Upload" />
  </form>
  `);
});

server.listen(1111, () => {
  console.log('Server listening on http://localhost:1111/ ...');
});

Node.js Express.js Server as middleware

const moduleName = 'Express';
const express = require('express');
const uploader = require('multi-part-form-data-upload');

const app = express();
const port = 1111;

app.post('/uploads', uploader(/* configs */), (req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ body: req.body }));
});

app.get('*', (req, res) => {
  res.send(`
  <h1>MultiPartFormData with Node.js <code>"${moduleName}"</code> module</h1>

  <form action="/uploads" enctype="multipart/form-data" method="post">
    <div>Text field title: <input type="text" name="title" /></div>
    <div>MultipleFiles: <input type="file" name="multipleFiles" multiple="multiple" /></div>
    <div>File1: <input type="file" name="file1" /></div>
    <div>File2: <input type="file" name="file2"  /></div>
    <input type="submit" value="Upload" />
  </form>
`);
});

app.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}/ ...`);
});

API Reference

Options

| Parameter | Type | Description | | :-------- | :------- | :------------------------- | | destination | string | destination folder path, default: temp folder | | filename | function | function(file, cb){ return cb(err/null, filename)}| | allowedExtensions | array | array of allowed extension types, default ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'webp', 'doc'] | | maxSize | integer | max file size in bytes, default 10000000. (10MB)

Options.filename : args

const uploader = require('../src/index')(
    {
        filename:function(file, cb){
            return cb(err, filename)
        }
    }
 );
file:(object)

| Property | Type | Description | | :-------- | :------- | :------------------------- | | filename | string | original file name | | mimetype | string | file mimetype| | size | number | size in bytes | | encoding | string | file encoding used| | destination | string | file upload folder| | path | string | complete path to saved file|

cb: callback function

| Argument | Type | Description | | :-------- | :------- | :------------------------- | | err | error|null | pass error or null | | filename | string | final filename for the file|

Author: @hsk11


Twitter Follow Linkedin: Harpal Singh GitHub followers