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 🙏

© 2026 – Pkg Stats / Ryan Hefner

common-file-upload

v1.1.7

Published

Common File Upload Middleware. To upload file locally.

Downloads

58

Readme

Common File Upload Middleware

This is a Node.js middleware for handling file uploads, built on top of Express.js. It allows you to upload files with configurable validation and save them to a specified directory.

Features

  • Flexible Configuration: Define multiple file upload fields with specific rules.
  • Validation Support: Enforce file types, number of files, and required fields.
  • Easy Integration: Plug-and-play middleware for Express.js applications.

Installation

Install the middleware using npm:

npm install common-file-upload

Usage example with detailed comments

Middleware using Express:

import { fileUpload, fileRead } from "common-file-upload";
import express from "express";

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// File upload configuration
const uploadConfig = [
	{
		field: "file", // The field name in the form-data
		uploadPath: "./uploads", // Directory to save the uploaded files
		validation: {
			required: true,
			min: 1,
			max: 5, // Maximum 5 files for this field
			filter: ["image/jpeg", "image/png"] // Allowed MIME types
		}
	}
];

// Define a route with file upload
app.post("/upload", fileUpload(uploadConfig), (req, res) => {
	// Access uploaded files
	const files = req.files;

	res.json({
		message: "Files uploaded successfully!",
		files
	});
});

// File read configuration
const readConfig = [
	{
		field: "file", // The field name in the form-data
		fileSize: 10 * 1024 * 1024, // Maximum file size: 10MB
		validation: {
			required: true,
			min: 1,
			max: 3, // Maximum 3 files for this field
			filter: ["application/pdf", "image/png"] // Allowed MIME types
		}
	}
];

// Define a route with file read
app.post("/read", fileRead(readConfig), (req, res) => {
	res.json({
		message: "Files read successfully!",
		files: req.files
	});
});

// Start the server
const PORT = 3030;
app.listen(PORT, () => {
	console.log(`Server is running on http://localhost:${PORT}`);
});

Example Usage of Custom response options (option) :

const option = {
	responses: {
		invalidField: {
			statusCode: 400,
			data: { success: false, message: "Unexpected file field or too many files uploaded." }
		},
		multerError: {
			statusCode: 500,
			data: { success: false, message: "File upload error occurred." }
		},
		requiredRes: {
			statusCode: 400,
			data: { success: false, message: "File is required." }
		},
		limitMin: {
			statusCode: 400,
			data: { success: false, message: "At least 1 file is required." }
		},
		limitMax: {
			statusCode: 400,
			data: { success: false, message: "Only 1 file is allowed." }
		}
	}
};

fileUpload(uploadConfig, option);

Pass the option object when calling the fileUpload function:

import { fileUpload, fileRead } from "common-file-upload";

app.post("/upload", fileUpload(uploadConfig, option), (req, res) => {
	// Access uploaded files
	const files = req.files;

	res.json({
		message: "Files uploaded successfully!",
		files
	});
});

Configuration Options

The middleware accepts an array of configurations for each file upload field:

Field Configuration

| Option | Type | Description | | ------------ | -------- | ------------------------------------------------------ | | field | string | The name of the form-data field for the file. | | uploadPath | string | Directory path where the uploaded files will be saved. | | validation | object | Validation rules for the file upload (see below). |

Validation Rules

| Option | Type | Description | | ---------- | ---------- | ----------------------------------------------------------- | | required | boolean | Whether the field is required. | | min | number | Minimum number of files to upload. | | max | number | Maximum number of files to upload. | | filter | string[] | Array of allowed MIME types (e.g.., image/jpeg, image/png). |

Custom Response Options (option)

You can also define custom responses for various error scenarios such as invalid file fields, Multer errors, or required files not being uploaded. These responses are passed as an option object to the fileUpload middleware.

option Object

The option object contains custom responses for different error conditions:

| Option | Type | Description | | -------------- | -------- | --------------------------------------------------------------------------- | | responses | object | Custom response configurations for errors. | | invalidField | object | Response when an unexpected file field or too many files are uploaded. | | multerError | object | Response for general Multer errors such as file size limits or disk errors. | | requiredRes | object | Response when a required file is not uploaded. | | limitMin | object | Response when the number of uploaded files is less than the minimum. | | limitMax | object | Response when the number of uploaded files exceeds the maximum allowed. |

API Reference

fileUpload(config)

Parameters:

  • config (Array): An array of file upload configurations.
  • option (Object, Optional): An object containing custom error responses.

fileRead(config, option)

Parameters:

  • config (Array): An array of file upload configurations.
  • option (Object, Optional): An object containing custom error responses.

Returns:

  • Middleware function for handling file uploads.

License

This project is licensed under the MIT License.