1.1.0 • Published 7 months ago
secure-file-upload v1.1.0
Secure File Upload
A robust middleware for secure file uploads in Node.js applications, with built-in support for file validation, encryption, and uploading to multiple storage providers (AWS S3, local storage).
Features
- File type and size validation using
Yup
. - Built-in support for AWS S3 and local file storage.
- Optional AES-256 encryption for sensitive files.
- Lightweight and extensible middleware for Express.js.
- Full TypeScript support.
Installation
Using NPM
npm install secure-file-upload
3. Testing the Enhanced Package
3.1. Example Usage in an Express App
Here's how you can use the enhanced package in an Express application.
// app.ts
import express from "express";
import dotenv from "dotenv";
import { secureFileUpload, generatePresignedUrl } from "secure-file-upload";
dotenv.config();
const app = express();
// Ensure the uploads directory exists for local storage
import fs from "fs";
if (!fs.existsSync("./uploads")) {
fs.mkdirSync("./uploads");
}
// File Upload Endpoint
app.post(
"/upload",
secureFileUpload({ storage: "s3" }), // Change to 'local' for local uploads
(req, res) => {
res.send("File uploaded securely!");
}
);
// Presigned URL Endpoint
app.get(
"/file/:key",
generatePresignedUrl({ expiresInSeconds: 600 }), // URL valid for 10 minutes
(req, res) => {
// The middleware handles the response
}
);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});