upload-express-middleware 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/uploadMiddleware.d.ts +3 -0
- package/dist/middleware/uploadMiddleware.d.ts.map +1 -0
- package/dist/middleware/uploadMiddleware.js +33 -0
- package/dist/middleware/uploadMiddleware.js.map +1 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Express Upload Middleware
|
|
2
|
+
|
|
3
|
+
A simple and flexible Express middleware for handling file uploads using Multer.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install express-upload
|
|
9
|
+
# or
|
|
10
|
+
pnpm add express-upload
|
|
11
|
+
# or
|
|
12
|
+
yarn add express-upload
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import express from 'express';
|
|
19
|
+
import { uploadMiddleware } from 'express-upload';
|
|
20
|
+
|
|
21
|
+
const app = express();
|
|
22
|
+
|
|
23
|
+
// Use the upload middleware with a specific folder
|
|
24
|
+
app.post('/upload', uploadMiddleware('uploads'), (req, res) => {
|
|
25
|
+
if (!req.file) {
|
|
26
|
+
return res.status(400).json({ error: 'No file uploaded' });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
res.json({
|
|
30
|
+
message: 'File uploaded successfully',
|
|
31
|
+
filename: req.file.filename,
|
|
32
|
+
path: req.file.path
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
app.listen(3000, () => {
|
|
37
|
+
console.log('Server running on port 3000');
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
- Dynamic folder creation
|
|
44
|
+
- Unique filename generation
|
|
45
|
+
- File size limit (5MB by default)
|
|
46
|
+
- TypeScript support
|
|
47
|
+
- Configurable upload destinations
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### `uploadMiddleware(folderName: string)`
|
|
52
|
+
|
|
53
|
+
Creates a multer middleware for handling single file uploads.
|
|
54
|
+
|
|
55
|
+
**Parameters:**
|
|
56
|
+
- `folderName` (string): The name of the folder where files will be stored
|
|
57
|
+
|
|
58
|
+
**Returns:**
|
|
59
|
+
- Express middleware function
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
ISC
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAGpE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/D,YAAY,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uploadMiddleware.d.ts","sourceRoot":"","sources":["../../src/middleware/uploadMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/E,eAAO,MAAM,gBAAgB,GAAI,YAAY,MAAM,KAAG,cAoCrD,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import multer from 'multer';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
// Middleware factory: folderName dynamic
|
|
5
|
+
export const uploadMiddleware = (folderName) => {
|
|
6
|
+
// Folder path
|
|
7
|
+
const uploadPath = path.join(__dirname, '..', folderName);
|
|
8
|
+
// Create folder if it doesn't exist
|
|
9
|
+
if (!fs.existsSync(uploadPath)) {
|
|
10
|
+
fs.mkdirSync(uploadPath, { recursive: true });
|
|
11
|
+
}
|
|
12
|
+
// Multer storage config
|
|
13
|
+
const storage = multer.diskStorage({
|
|
14
|
+
destination: (req, file, cb) => {
|
|
15
|
+
cb(null, uploadPath);
|
|
16
|
+
},
|
|
17
|
+
filename: (req, file, cb) => {
|
|
18
|
+
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
|
|
19
|
+
const originalName = file.originalname.replace(/\s+/g, '_');
|
|
20
|
+
cb(null, uniqueSuffix + '-' + originalName);
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
// Optional: File filter
|
|
24
|
+
const fileFilter = (req, file, cb) => {
|
|
25
|
+
// Accept all files for now, you can filter by mime type
|
|
26
|
+
cb(null, true);
|
|
27
|
+
};
|
|
28
|
+
// Max file size 5MB
|
|
29
|
+
const limits = { fileSize: 5 * 1024 * 1024 };
|
|
30
|
+
// Return the multer middleware
|
|
31
|
+
return multer({ storage, fileFilter, limits }).single('file'); // 'file' is the field name in form-data
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=uploadMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uploadMiddleware.js","sourceRoot":"","sources":["../../src/middleware/uploadMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,yCAAyC;AACzC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,UAAkB,EAAkB,EAAE;IACrE,cAAc;IACd,MAAM,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,CAAC,CAAC,wCAAwC;AACzG,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "upload-express-middleware",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Express middleware for file uploads using multer",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/**/*"
|
|
10
|
+
],
|
|
11
|
+
"keywords": [
|
|
12
|
+
"express",
|
|
13
|
+
"upload",
|
|
14
|
+
"multer",
|
|
15
|
+
"middleware",
|
|
16
|
+
"file-upload"
|
|
17
|
+
],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/express": "^5.0.6",
|
|
22
|
+
"@types/multer": "^2.0.0",
|
|
23
|
+
"@types/node": "^25.0.3",
|
|
24
|
+
"nodemon": "^3.1.11",
|
|
25
|
+
"rimraf": "^6.1.2",
|
|
26
|
+
"ts-node": "^10.9.2",
|
|
27
|
+
"typescript": "^5.9.3"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"express": "^5.2.1",
|
|
31
|
+
"multer": "^2.0.2"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18.0.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc",
|
|
38
|
+
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
|
|
39
|
+
"start": "node dist/index.js",
|
|
40
|
+
"clean": "rimraf dist"
|
|
41
|
+
}
|
|
42
|
+
}
|