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

upmulp

v1.2.0

Published

Custom multipart form data parser

Downloads

10

Readme

upmulp

Inspired by the need to handle multipart form data in Node.js applications when multer or busboy were not suitable. This package helps you parse multipart form data in Node.js applications, allowing you to handle file uploads and form fields with ease.

Features

  • Parses multipart form data
  • Handles file uploads
  • Extracts form fields
  • Easy integration with Express.js

Installation

Install the package using npm:

npm install upmulp

Usage

Basic Usage

Here's an example of how to use upmulp in an Express application:

const express = require("express");
const path = require("path");
const fs = require("fs");
const MultipartParser = require("upmulp");

const app = express();

const uploadDir = path.join(__dirname, "uploads");
if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir);
}

app.post("/upload", (req, res) => {
  let parser;
  try {
    parser = MultipartParser.fromRequest(req);
  } catch (err) {
    return res.status(400).send(err.message);
  }

  parser.parse(req, (err, result) => {
    if (err) {
      return res.status(500).send("Error parsing form data");
    }

    console.log("Files:", result.files);
    console.log("Fields:", result.fields);
    res.status(200).send("Upload successful");
  });
});

MultipartParser Class

The MultipartParser class is responsible for parsing multipart form data. Below are its main methods and properties.

Constructor

const parser = new MultipartParser(boundary);

boundary: The boundary string used to separate parts in the multipart form data.

Methods

parse(req, callback)

Parses the incoming request stream.

  • req: The HTTP request object.
  • callback: A callback function that receives an error (if any) and the parsed result.

Example:

parser.parse(req, (err, result) => {
  if (err) {
    console.error("Error parsing form data:", err);
  } else {
    console.log("Parsed fields:", result.fields);
    console.log("Parsed files:", result.files);
  }
});

Result Object

The result object passed to the callback contains two properties:

  • fields: An object containing form fields.

  • files: An array of file objects, each with the following properties:

    • name: The name of the form field.
    • path: The path to the uploaded file on the server.

Compatibility

This package supports both CommonJS and ES module environments. You can import it using either require or import syntax:

CommonJS:

const MultipartParser = require("upmulp");

ES Module:

import MultipartParser from "upmulp";

Development

Running Tests

To run the tests, use the following command:

npm test

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes.
  4. Commit your changes (git commit -am 'Add new feature').
  5. Push to the branch (git push origin feature-branch).
  6. Create a new pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

  • Inspired by the need to handle multipart form data in Node.js applications.
  • Uses concepts from existing multipart parsers like multer and busboy.

Thank you for using upmulp! If you have any questions or need further assistance, feel free to open an issue or contact me.