upload-files-express
Advanced tools
Comparing version
22
index.js
const formidable = require("formidable"); | ||
module.exports = function(options = {}) { | ||
return function(req, res, next) { | ||
module.exports = function (options = {}) { | ||
return function (req, res, next) { | ||
// These cannot have a body at all, so don't even attempt it | ||
@@ -22,6 +22,10 @@ if (req.method === "GET" || req.method === "DELETE") return next(); | ||
const form = new formidable.IncomingForm(options); | ||
if (options.hash) { | ||
options.hashAlgorithm = options.hash; | ||
} | ||
form.parse(req, function(err, fields, files) { | ||
if (err) next(err); | ||
const form = formidable(options); | ||
form.parse(req, function (err, fields, files) { | ||
if (err) return next(err); | ||
req.body = fields; | ||
@@ -31,7 +35,7 @@ req.files = {}; | ||
req.files[file] = { | ||
path: files[file].path, | ||
name: files[file].name, | ||
type: files[file].type, | ||
path: files[file].filepath, | ||
name: files[file].originalFilename, | ||
type: files[file].mimetype, | ||
size: files[file].size, | ||
modified: files[file].lastModifiedDate | ||
modified: files[file].lastModifiedDate, | ||
}; | ||
@@ -38,0 +42,0 @@ } |
{ | ||
"name": "upload-files-express", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "An easy way to handle file uploads on the server with express", | ||
@@ -29,3 +29,3 @@ "homepage": "https://github.com/franciscop/upload-files-express#readme", | ||
"dependencies": { | ||
"formidable": "^1.0.17" | ||
"formidable": "^2.0.1" | ||
}, | ||
@@ -32,0 +32,0 @@ "devDependencies": { |
@@ -6,3 +6,3 @@ # Upload files express | ||
```js | ||
const uploadFiles = require('upload-files-express'); | ||
const uploadFiles = require("upload-files-express"); | ||
@@ -12,3 +12,3 @@ // Pass any options that you want here: | ||
app.post('/form', (req, res) => { | ||
app.post("/form", (req, res) => { | ||
// The key is the name="" in the original form | ||
@@ -32,4 +32,2 @@ console.log(req.files); | ||
## Getting started | ||
@@ -46,4 +44,4 @@ | ||
```js | ||
const express = require('express'); | ||
const uploadFiles = require('upload-files-express'); | ||
const express = require("express"); | ||
const uploadFiles = require("upload-files-express"); | ||
@@ -57,13 +55,13 @@ const app = express(); | ||
## Options | ||
It uses `formidable` to parse the data, so [you can use any of formidable's configuration options](https://github.com/felixge/node-formidable#api). Pass the options with an object: | ||
It uses `formidable` to parse the data, so [you can use any of formidable 2 configuration options](https://github.com/node-formidable/formidable/tree/v2-latest#options). Pass the options with an object: | ||
```js | ||
app.use(uploadFiles({ | ||
uploadDir: './uploads', | ||
maxFileSize: 10 * 1024 * 1024 // ~10 MB | ||
})); | ||
app.use( | ||
uploadFiles({ | ||
uploadDir: "./uploads", | ||
maxFileSize: 10 * 1024 * 1024, // ~10 MB | ||
}) | ||
); | ||
``` | ||
@@ -79,2 +77,4 @@ | ||
extensions of the original files or not | ||
- `allowEmptyFiles` **{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); | ||
@@ -87,6 +87,5 @@ limit the size of uploaded file. | ||
bytes. | ||
- `hash` **{boolean}** - default `false`; include checksums calculated | ||
for incoming files, set this to some hash algorithm, see | ||
[crypto.createHash](https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options) | ||
for available algorithms | ||
- `hashAlgorithm` **{string|boolean}** - default `false`; include checksums calculated for incoming files, set this to some hash algorithm, see crypto.createHash for available algorithms | ||
- `hash` - 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 | ||
@@ -97,16 +96,18 @@ `.parse` method, the `files` argument (of the callback) will contain arrays of | ||
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. | ||
> Note: the `keepExtensions` defaults to `true` instead of `false` as in formidable. | ||
> Notes: the `keepExtensions` defaults to `true` instead of `false` 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! | ||
## Upload files to S3, GCS, Backblaze's B2 | ||
You likely want to upload your files to a 3rd party storage service, since most Node.js servers have [an ephemeral filesystem](https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted) so all the data will be removed on the next deploy. | ||
You likely want to upload your files to a 3rd party storage service, since most Node.js servers have [an ephemeral filesystem](https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted) 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`: | ||
```js | ||
const uploadFiles = require('upload-files-express'); | ||
const service = require('some-service'); | ||
const uploadFiles = require("upload-files-express"); | ||
const service = require("some-service"); | ||
@@ -116,3 +117,3 @@ app.use(uploadFiles()); | ||
// We made the callback async to be able to `await` on it inside | ||
app.post('/form', async (req, res, next) => { | ||
app.post("/form", async (req, res, next) => { | ||
try { | ||
@@ -136,3 +137,2 @@ // Still using the same form. Now we wait for the file to upload and keep | ||
// ... Save in DB, respond, etc. here | ||
} catch (error) { | ||
@@ -144,4 +144,2 @@ next(error); | ||
## Upload image to MongoDB | ||
@@ -153,5 +151,5 @@ | ||
// Using mongoose here | ||
const User = mongoose.model('User', userSchema); | ||
const User = mongoose.model("User", userSchema); | ||
app.post('/form', async (req, res, next) => { | ||
app.post("/form", async (req, res, next) => { | ||
try { | ||
@@ -170,4 +168,2 @@ // ... Same as before here | ||
## Author & License | ||
@@ -174,0 +170,0 @@ |
9725
16.29%38
8.57%161
-2.42%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
Updated