Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
upload-files-express
Advanced tools
An easy way to handle file uploads on the server with express
An easy way to handle file uploads on the server with express. The files are saved in the filesystem and put in req.files
:
const uploadFiles = require("upload-files-express");
// Pass any options that you want here:
app.use(uploadFiles());
app.post("/form", (req, res) => {
// The key is the name="" in the original form
console.log(req.files);
// {
// profile: {
// path: '/tmp/69793b826f1c9685.png', // Where the file is saved
// name: 'profile.png', // The original file name
// type: 'image/png' // The MIME type of file
// size: 5940, // The bytes of the file
// modified: 2020-06-03T18:53:20.687Z // The Date() the file was uploaded
// }
// }
// ...
});
This module also parses the rest of the form, so
body-parser
is not needed 🎉
First install it with npm:
npm install upload-files-express
Then you have to import it and load it as an express middleware in your server:
const express = require("express");
const uploadFiles = require("upload-files-express");
const app = express();
app.use(uploadFiles());
// ...
It uses formidable
to parse the data, so you can use any of formidable 2 configuration options. Pass the options with an object:
app.use(
uploadFiles({
uploadDir: "./uploads",
maxFileSize: 10 * 1024 * 1024, // ~10 MB
})
);
These are all the available options:
encoding
{string} - default 'utf-8'
; sets encoding for
incoming form fields,uploadDir
{string} - default os.tmpdir()
; the directory for
placing file uploads in, which must exist previously. You can move them later by using fs.rename()
keepExtensions
{boolean} - default true
; to include the
extensions of the original files or notallowEmptyFiles
{boolean} - default true
; allow upload empty files.
options.minFileSize {number} - default 1 (1byte); the minium size of uploaded file.maxFileSize
{number} - default 200 * 1024 * 1024
(200mb);
limit the size of uploaded file.maxFields
{number} - default 1000
; limit the number of fields
that the Querystring parser will decode, set 0 for unlimitedmaxFieldsSize
{number} - default 20 * 1024 * 1024
(20mb);
limit the amount of memory all fields together (except files) can allocate in
bytes.hashAlgorithm
{string|boolean} - default false
; include checksums calculated for incoming files, set this to some hash algorithm, see crypto.createHash for available algorithmshash
- kept for compat reasons; see hashAlgorithm
.fileWriteStreamHandler
{function} - default null
; which by default writes to host machine file system every file parsed; The function should return an instance of a Writable stream that will receive the uploaded file data. With this option, you can have any custom behavior regarding where the uploaded file data will be streamed for. If you are looking to write the file uploaded in other types of cloud storages (AWS S3, Azure blob storage, Google cloud storage) or private file storage, this is the option you're looking for. When this option is defined the default behavior of writing the file in the host machine file system is lost.multiples
{boolean} - default false
; when you call the
.parse
method, the files
argument (of the callback) will contain arrays of
files for inputs which submit multiple files using the HTML5 multiple
attribute. Also, the fields
argument will contain arrays of values for
fields that have names ending with '[]'.filename
{function} - default undefined
Use it to control newFilename. Must return a string. Will be joined with options.uploadDir.filter
{function} - default function that always returns true. Use it to filter files before they are uploaded. Must return a boolean.Notes: the
keepExtensions
defaults totrue
instead offalse
as in formidable.hash
is kept for back-compat reasons
Note: with the latest version, you probably want to use
fileWriteStreamHandler
option to avoid touching the local filesystem!
You likely want to upload your files to a 3rd party storage service, since most Node.js servers have an ephemeral filesystem so all the data will be removed on the next deploy.
To keep our files we can upload these to S3, Backblaze's B2, Google's GCS, etc. We are using a fictitious service here some-service
:
const uploadFiles = require("upload-files-express");
const service = require("some-service");
app.use(uploadFiles());
// We made the callback async to be able to `await` on it inside
app.post("/form", async (req, res, next) => {
try {
// Still using the same form. Now we wait for the file to upload and keep
// the resulting filename as a result. This workflow will be different
// depending on which service you use:
const localFile = req.files.profile.path;
const name = await service.upload(localFile);
// A full user profile example:
const userData = { ...req.body, profile: `https://service.com/${name}` };
// Now "userData" has all the data and the link to the image we want
console.log(user);
// {
// name: 'Francisco',
// profile: 'https://service.com/fkjfdinuaef.png'
// }
// ... Save in DB, respond, etc. here
} catch (error) {
next(error);
}
});
While you cannot upload data straight to MongoDB, you can do the previous workflow and then upload the image reference to MongoDB or any other database that you prefer:
// Using mongoose here
const User = mongoose.model("User", userSchema);
app.post("/form", async (req, res, next) => {
try {
// ... Same as before here
const user = new User(userData);
await user.save();
return res.json(userData);
} catch (error) {
next(error);
}
});
Made by Francisco Presencia under the MIT License.
A small part of the docs coming from formidable, since that's a dependency for this library. This project was previously named express-data-parser
.
FAQs
An easy way to handle file uploads on the server with express
The npm package upload-files-express receives a total of 751 weekly downloads. As such, upload-files-express popularity was classified as not popular.
We found that upload-files-express demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.