Installations
npm install upload-files-express
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.0.0
NPM Version
8.6.0
Score
66.8
Supply Chain
98.3
Quality
75
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
franciscop
Download Statistics
Total Downloads
429,163
Last Day
289
Last Week
1,743
Last Month
13,486
Last Year
232,610
GitHub Statistics
4 Stars
13 Commits
3 Watching
1 Branches
1 Contributors
Sponsor this package
Package Meta Information
Latest Version
0.4.0
Package Id
upload-files-express@0.4.0
Unpacked Size
9.50 kB
Size
4.16 kB
File Count
4
NPM Version
8.6.0
Node Version
18.0.0
Total Downloads
Cumulative downloads
Total Downloads
429,163
Last day
-19%
289
Compared to previous day
Last week
-46%
1,743
Compared to previous week
Last month
-2.4%
13,486
Compared to previous month
Last year
23.7%
232,610
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
1
Upload files 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
:
1const uploadFiles = require("upload-files-express"); 2 3// Pass any options that you want here: 4app.use(uploadFiles()); 5 6app.post("/form", (req, res) => { 7 // The key is the name="" in the original form 8 console.log(req.files); 9 // { 10 // profile: { 11 // path: '/tmp/69793b826f1c9685.png', // Where the file is saved 12 // name: 'profile.png', // The original file name 13 // type: 'image/png' // The MIME type of file 14 // size: 5940, // The bytes of the file 15 // modified: 2020-06-03T18:53:20.687Z // The Date() the file was uploaded 16 // } 17 // } 18 19 // ... 20});
This module also parses the rest of the form, so
body-parser
is not needed 🎉
Getting started
First install it with npm:
1npm install upload-files-express
Then you have to import it and load it as an express middleware in your server:
1const express = require("express"); 2const uploadFiles = require("upload-files-express"); 3 4const app = express(); 5 6app.use(uploadFiles()); 7 8// ...
Options
It uses formidable
to parse the data, so you can use any of formidable 2 configuration options. Pass the options with an object:
1app.use( 2 uploadFiles({ 3 uploadDir: "./uploads", 4 maxFileSize: 10 * 1024 * 1024, // ~10 MB 5 }) 6);
These are all the available options:
encoding
{string} - default'utf-8'
; sets encoding for incoming form fields,uploadDir
{string} - defaultos.tmpdir()
; the directory for placing file uploads in, which must exist previously. You can move them later by usingfs.rename()
keepExtensions
{boolean} - defaulttrue
; to include the extensions of the original files or notallowEmptyFiles
{boolean} - defaulttrue
; allow upload empty files. options.minFileSize {number} - default 1 (1byte); the minium size of uploaded file.maxFileSize
{number} - default200 * 1024 * 1024
(200mb); limit the size of uploaded file.maxFields
{number} - default1000
; limit the number of fields that the Querystring parser will decode, set 0 for unlimitedmaxFieldsSize
{number} - default20 * 1024 * 1024
(20mb); limit the amount of memory all fields together (except files) can allocate in bytes.hashAlgorithm
{string|boolean} - defaultfalse
; include checksums calculated for incoming files, set this to some hash algorithm, see crypto.createHash for available algorithmshash
- kept for compat reasons; seehashAlgorithm
.fileWriteStreamHandler
{function} - defaultnull
; 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} - defaultfalse
; when you call the.parse
method, thefiles
argument (of the callback) will contain arrays of files for inputs which submit multiple files using the HTML5multiple
attribute. Also, thefields
argument will contain arrays of values for fields that have names ending with '[]'.filename
{function} - defaultundefined
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
Upload files to S3, GCS, Backblaze's B2
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
:
1const uploadFiles = require("upload-files-express"); 2const service = require("some-service"); 3 4app.use(uploadFiles()); 5 6// We made the callback async to be able to `await` on it inside 7app.post("/form", async (req, res, next) => { 8 try { 9 // Still using the same form. Now we wait for the file to upload and keep 10 // the resulting filename as a result. This workflow will be different 11 // depending on which service you use: 12 const localFile = req.files.profile.path; 13 const name = await service.upload(localFile); 14 15 // A full user profile example: 16 const userData = { ...req.body, profile: `https://service.com/${name}` }; 17 18 // Now "userData" has all the data and the link to the image we want 19 console.log(user); 20 // { 21 // name: 'Francisco', 22 // profile: 'https://service.com/fkjfdinuaef.png' 23 // } 24 25 // ... Save in DB, respond, etc. here 26 } catch (error) { 27 next(error); 28 } 29});
Upload image to MongoDB
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:
1// Using mongoose here 2const User = mongoose.model("User", userSchema); 3 4app.post("/form", async (req, res, next) => { 5 try { 6 // ... Same as before here 7 8 const user = new User(userData); 9 await user.save(); 10 11 return res.json(userData); 12 } catch (error) { 13 next(error); 14 } 15});
Author & License
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
.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/13 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2024-12-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to upload-files-express
express-fileupload
Simple express file upload middleware that wraps around Busboy
skipper
Bodyparser for Express/Sails. Exposes simple API for streaming multiple files to disk, S3, etc. without buffering to a .tmp directory.
express-mustache-upload
Upload files to a directory
@fatchan/express-fileupload
Simple express file upload middleware that wraps around Busboy