Logo

dev-resources.site

for different kinds of informations.

Handling Image Uploads in Node with Multer and Cloudinary

Published at
1/4/2024
Categories
node
multe
cloudinary
images
Author
gilbertintabo
Categories
4 categories in total
node
open
multe
open
cloudinary
open
images
open
Author
13 person written this
gilbertintabo
open
Handling Image Uploads in Node with Multer and Cloudinary

Hey there! πŸ‘‹ I recently ran into a bit of a challenge while creating my portfolio. I had a blog page with cover images, and initially, I used Multer to upload images to a local folder on my server. Everything worked like a charm until I decided to host my site on Render directly from GitHub. The images weren't uploading to GitHub, which wasn't surprising.

I decided to use cloud-based storage and opted for Cloudinary. In this guide, I'll walk you through how I set up Cloudinary on my server integrated it with Multer, and saved the image links to MongoDB.Solving my problem.

I started by installing the dependencies.

npm install multer cloudinary dotenv
Enter fullscreen mode Exit fullscreen mode

Before diving into code, I had to set up Cloudinary account and grab my API key, API secret, and cloud name from the dashboard. I added a .env file in my server's root folder and populated it like this:

CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
CLOUDINARY_CLOUD_NAME=your_cloud_name
Enter fullscreen mode Exit fullscreen mode

Next, I created a utils folder and within it, a file named cloudinary.js to configure my Cloudinary setup:

// utils/cloudinary.js

const cloudinary = require('cloudinary').v2;
require('dotenv').config();

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});

module.exports = cloudinary;

Enter fullscreen mode Exit fullscreen mode

Now, in my root folder, I set up a middleware folder, and within it I Created a file named multer.js to configure Multer:

// multer.js

const multer = require('multer');

const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

module.exports = upload;
Enter fullscreen mode Exit fullscreen mode

imported these two files into my post controllers like this:

// Your post controllers file


const cloudinary = require('./utils/cloudinary');
const upload = require('./multer');
Enter fullscreen mode Exit fullscreen mode

Now, in my post route, I can upload a file and wait until it's uploaded using a promise before adding the rest of the form data from the body. Here's how my post route looks:

my post route

my post route body

images Article's
30 articles in total
Favicon
Proxy de imΓ‘genes.
Favicon
Deploying Mancala with Minikube β€” A beginners guide
Favicon
Collect All Requested Images on a Website Using Puppeteer
Favicon
I’m Building a Website Builder Tool, and I Created a Simple Placeholder Image Service Along the Way
Favicon
Enhancing Data Collection Images AI Accuracy and Performance
Favicon
Dynamic watermarking on the JVM
Favicon
Understanding Dockerfile: The Blueprint of Docker Containers and Images
Favicon
A simple Image to text website built with Next.js
Favicon
Error while loading images in Oracle APEX application runtime
Favicon
Base 64
Favicon
Alpaca image generator website Built with ReactJS
Favicon
Use your own neural net to generate images
Favicon
How to calculate RGB values in Python
Favicon
More Fun with AI Generated Images
Favicon
Useful websites for stock images
Favicon
Optimizing Images for Enhanced Website Performance: A Free Tool for Your Website
Favicon
RuGiVi
Favicon
A Simple Approach to LCP Image Optimization with TwicPics Components
Favicon
Fix: Opencart cache images not generated
Favicon
Keeping your fonts in embedded SVG
Favicon
Upload the image with a preview using HTML, CSS & JavaScript
Favicon
Handling Image Uploads in Node with Multer and Cloudinary
Favicon
Before and after image slider in pure CSS
Favicon
Product Page Images Grid
Favicon
Convert images in Webp
Favicon
🎁 Collection of free illustrations for OpenGraph images
Favicon
Create High Quality Images And Videos With GenmoAI
Favicon
Instantly adds nice compression artifacts to your JPEG images
Favicon
React Image Pan And Zoom With Commenting
Favicon
Bring back the Twitter link preview

Featured ones: