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

express-file-bucket

v1.3.0

Published

Small file upload, delete, and URL helper for Express applications; supports CommonJS and ES modules.

Readme

express-file-bucket

A small helper for saving uploaded files, deleting saved files, and building public URLs in Express apps. It supports CommonJS and ES modules, and accepts express-fileupload file objects or common Multer file objects.

Install

npm install express-file-bucket

You also need an upload middleware, for example:

npm install express-fileupload
// app.js (ES module)
import express from 'express';
import fileUpload from 'express-fileupload';
import { saveFiles, deleteFiles, getFiles } from 'express-file-bucket';

const app = express();
app.use(fileUpload());
app.use('/uploads', express.static('public/uploads'));

app.post('/upload', async (req, res, next) => {
  try {
    const names = await saveFiles(req.files?.files ?? req.files?.file);
    res.json({ files: names, urls: getFiles(req, 'uploads', names) });
  } catch (error) {
    next(error);
  }
});

CommonJS

const { saveFiles, deleteFiles, getFiles } = require('express-file-bucket');

API

await saveFiles(fileOrFiles, options?)

Save one file or an array of files. Always returns an array of saved basenames. With no options, files go to public/uploads and keep their original filename unless that name already exists, in which case a random suffix is added to avoid overwriting.

const names = await saveFiles(req.files.avatar);
const many = await saveFiles(req.files.documents);

Options:

| Option | Type | Default | Meaning | | --- | --- | --- | --- | | upload_path | string | public/uploads | Local directory to save into | | file_name | string | original filename | Optional exact desired filename; preserved exactly as supplied, including its extension (or lack of one) | | fixed_file_name | string | — | Backwards-compatible fixed-name option; can overwrite only when overwrite: true | | keep_original_name | boolean | false | Keep each original filename | | allowed_ext | string[] | — | Allow only these extensions, e.g. ['jpg', 'png'] | | min_size | number | — | Minimum size in bytes | | max_size | number | — | Maximum size in bytes | | overwrite | boolean | false | Allow writing over the requested destination name |

Examples:

// Save using the exact requested name (extension included)
await saveFiles(req.files.avatar, { file_name: 'school-logo.png' });

// The name is preserved exactly as supplied (no extension is added)
await saveFiles(req.files.avatar, { file_name: 'profile-photo' });

// Custom directory and validation
await saveFiles(req.files.documents, {
  upload_path: 'public/uploads/documents',
  allowed_ext: ['pdf', 'docx'],
  max_size: 5 * 1024 * 1024
});

// Keep each original filename; existing files are not overwritten
await saveFiles(req.files.documents, { keep_original_name: true });

If a name already exists and overwrite is false, the package adds a suffix to prevent accidental replacement. To deliberately replace a file at a known name, set overwrite: true and provide file_name or fixed_file_name.

await deleteFiles(folder, filenameOrFilenames)

Delete one or many files from a directory. It returns an array of successfully deleted basenames; missing files are ignored. It does not recursively delete directories.

await deleteFiles('public/uploads', 'school-logo.png');
await deleteFiles('public/uploads', ['one.pdf', 'two.png']);

Only basenames are accepted: paths supplied as filenames are reduced to their basename, preventing ../ traversal outside the selected folder.

getFiles(req, publicFolder, filenameOrFilenames)

Build absolute public URLs from the Express request. The folder argument is the URL path, not necessarily the disk path. Mount the matching static directory yourself.

app.use('/uploads', express.static('public/uploads'));
const urls = getFiles(req, 'uploads', ['school-logo.png', 'photo.jpg']);
// ['https://example.com/uploads/school-logo.png', ...]
const oneUrl = getFiles(req, 'uploads', 'school-logo.png');
// Returns an array with one URL, for consistency with the original API.

ensureDir(folder)

Creates a directory and any missing parent directories, then resolves with the folder path.

Supported upload middleware objects

  • express-fileupload: uses file.name, file.size, and file.mv(destination).
  • Multer: supports file.originalname with file.buffer (memory storage) or file.path (disk storage).

For Multer disk storage, this package copies the file to the destination; it does not remove Multer's temporary/source file.

Important notes

  • Upload validation errors reject the promise. Handle them with try/catch or Express error middleware.
  • allowed_ext checks the extension only; it does not inspect file contents or prove that a file is safe.
  • Configure upload middleware limits as well as package-level validation for production.
  • getFiles() only constructs URLs. The app must expose the directory with express.static() or another static-file handler.
  • The default path is relative to the process working directory. Set upload_path explicitly if your app uses a different root.