jQuery File Upload Express Middleware
raw JSON →This package provides an Express.js middleware solution to integrate with the client-side jQuery File Upload plugin. Designed for older Express.js (v3.x or early v4.x) and Node.js environments (specifically requiring Node.js >= 0.8.8), it facilitates server-side handling of file uploads, including image resizing for thumbnails. The latest version is 0.1.8, published approximately nine years ago. Due to its age and reliance on deprecated Express.js APIs like `app.configure()` and `express.bodyParser()`, it is not compatible with modern Express.js (v4.x+ or v5.x) or recent Node.js versions. Modern file upload solutions typically use dedicated multipart form data parsers like `multer` or the built-in `express.json()` and `express.urlencoded()` with separate file handling libraries.
Common errors
error TypeError: app.configure is not a function ↓
multer. error Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. ↓
body-parser can be installed for older Express 4.x versions, this particular middleware is still problematic. For file uploads, specifically, it's advised to use multer or similar dedicated middleware. If only JSON/URL-encoded bodies are needed, use app.use(express.json()); app.use(express.urlencoded({ extended: true }));. error Image uploads successfully, folder thumbs is created but there is no files inside. ↓
sudo apt-get install imagemagick. On macOS: brew install imagemagick. Also, ensure you have an upload.on('error', ...) listener to catch image processing exceptions. Warnings
breaking This package relies on Express.js 3.x or very early 4.x APIs. `app.configure()` was removed in Express 4.x, and `express.bodyParser()` was deprecated and replaced, making this middleware incompatible with modern Express.js (v4.x and v5.x). ↓
breaking The package requires Node.js >= 0.8.8, an extremely old version. It is unlikely to run correctly or securely on modern Node.js runtimes (e.g., Node.js 18+). ↓
gotcha When using `imageVersions` for thumbnail generation, ImageMagick must be installed on the system. Without it, image processing features will silently fail or throw errors that need explicit logging to catch. ↓
deprecated The original `express.bodyParser()` middleware, which this package's usage patterns align with, had security vulnerabilities related to the handling of temporary files, potentially leading to denial-of-service by filling disk space with uncleaned files for all POST requests. ↓
Install
npm install jquery-file-upload-middleware yarn add jquery-file-upload-middleware pnpm add jquery-file-upload-middleware Imports
- upload wrong
import upload from 'jquery-file-upload-middleware';correctconst upload = require('jquery-file-upload-middleware'); - configure wrong
const { configure } = require('jquery-file-upload-middleware'); configure({...});correctupload.configure({...}); - fileHandler wrong
app.use('/upload', fileHandler());correctapp.use('/upload', upload.fileHandler());
Quickstart
const express = require("express");
const upload = require('jquery-file-upload-middleware');
const app = express();
// IMPORTANT: For modern Express.js (v4.16.0+), use express.json() and express.urlencoded()
// For older Express versions, `express.bodyParser()` was used.
// This package expects an Express.js version where `app.configure` and `express.bodyParser` exist.
// This example is for *very old* Express.js environments.
// Configure upload middleware globally
upload.configure({
uploadDir: __dirname + '/public/uploads',
uploadUrl: '/uploads',
imageVersions: {
thumbnail: {
width: 80,
height: 80
}
}
});
// app.configure is deprecated/removed in Express 4.x and above
// This block would need to be re-written for modern Express
// For demonstration purposes, we show the original usage.
// In modern Express, middleware are usually applied directly or conditionally.
// Simulate old app.configure behavior for context
(function() {
// ... other middleware, e.g., session, cookie-parser ...
app.use('/upload', upload.fileHandler());
// express.bodyParser() is deprecated in modern Express
// For files, you should use a dedicated multipart parser like 'multer'
// For JSON/URL-encoded, use express.json() / express.urlencoded()
// app.use(express.bodyParser()); // DO NOT USE IN MODERN EXPRESS
// ...
})();
// Example of an event listener for successful uploads
upload.on('end', function (fileInfo, req, res) {
console.log('File uploaded:', fileInfo.name, 'to', fileInfo.url);
// In a real app, you might save fileInfo to a database or respond to the client
// Note: The original middleware might handle sending JSON response by itself.
});
// Example of an error listener
upload.on('error', function (e, req, res) {
console.error('Upload error:', e.message);
// You might want to send a more user-friendly error response
});
// Serve static files (e.g., the uploaded images)
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('<html><body><h1>File Upload Example</h1><input id="fileupload" type="file" name="files[]" data-url="/upload" multiple><script src="https://code.jquery.com/jquery-3.6.0.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/vendor/jquery.ui.widget.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.22.0/js/jquery.fileupload.min.js"></script><script>$('#fileupload').fileupload({ dataType: 'json' });</script></body></html>');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
console.log(`Upload directory: ${__dirname}/public/uploads`);
});