A simple Node.js middleware package for uploading files to Cloudinary using Multer and multer-storage-cloudinary.
Install the package and its required dependency:
npm install cloudinary-file-upload multer-storage-cloudinary
const express = require('express');
const dotenv = require('dotenv');
const getUploader = require('cloudinary-file-upload');
dotenv.config();
const app = express();
const PORT = 8000;
// Set up the uploader middleware with your desired Cloudinary folder
const upload = getUploader('my-folder-name');
// File upload endpoint
app.post('/upload', upload.single('file'), (req, res) => {
res.json({
message: 'File uploaded successfully!',
url: req.file.path,
});
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
- Start the server:
node index.js
- Open Postman or any API testing tool.
- Send a POST request to:
http://localhost:8000/upload
- In the Body, choose form-data.
- Key: file (make sure type is set to File)
- Value: Choose any image or file to upload.
{
"message": "File uploaded successfully!",
"url": "https://res.cloudinary.com/your_cloud_name/image/upload/v1234567890/my-folder-name/filename.jpg"
}