How to securely upload your image files to Cloud Storage using NodeJS.

nodeJs and cloudinary

Create an account with Cloudinary or other Cloud Storage platform of your choice.

cloudinary details on dashboard

Create a Node/Express Server to handle your image upload to Cloudinary.

initial index.js
  1. First require multer in the index.js file by doing
  2. const multer = require('multer');
  3. const upload = multer();
  4. Add multer as a middleware to the /cloud-upload route like this
  1. First, require cors in the index.js file by doing
  2. const cors = require('cors');
  3. Setup CORS whitelist to allow access to only your UI server
  4. Add the setup as a middleware to your /cloud-upload route
index.js with CORS restrictions
  1. First require datauri and path in the index.js file.
  2. Instantiate datauri; const dataUri = new DataUri();
  3. Setup the dataUri to transform our image data in buffer format to a readable url format that Cloudinary can understand.
  1. Add your cloud credentials to a .env file on the same level as the index.js
  2. Require dotenv
  3. Do dotenv.config(); to setup dotenv
Using postman

Setup your web app to use this endpoint for uploads

A Recap on What We have Covered

  1. Create an account with Cloudinary or other Cloud Storage platform of your choice and obtain the Account credentials required.
  2. Create Node/Express Server to handle your image upload to Cloudinary. Add multer to the file in your payload; adddatauri to convert the buffer object from your payload to a format that Cloudinary can read; add CORS to restrict access to your endpoint to only apps that your want;
  3. Setup your web app to use your upload API endpoint.

--

--

--

Software Engineer. Loves to play Coldplay on repeat while coding JavaScript and Python apps.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

TypeScript in 2021 and beyond — not worth your time

Keeping yourself productive as a Developer

The JavaScript Rookie How To— Part 1

Mongose | CRUD RESTful API Endpoints

CRUD on Document and Nested Sub-Document with MongoDB Atlas.

Difference between var, let and const in Javascript.

https://youtube.com/shorts/VmJXYLH0fLA?feature=share

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Theo Okafor

Theo Okafor

Software Engineer. Loves to play Coldplay on repeat while coding JavaScript and Python apps.

More from Medium

Databases for Web developers

SQL and NoSQL

Getting started with Mongoose, a beginner’s perspective

Introduction of Node.JS

Experiencing web application development as an undergraduate