npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rod-fileupload

v1.1.2

Published

middleware to upload file to cloudinary

Readme

Package Name

rod-fileupload

Description

An Express.js middleware that simplifies file uploads to Cloudinary by reducing the need for complex Cloudinary and Multer configurations. It handles file uploads from the client, uploads them to Cloudinary, and adds the resulting file information as an object to the request body for easy access in your application. it is package wich developed to be used in backend application of node js with express and typescript as they are growing tools in software industry

Installation

Install it via npm:

npm install rod-fileupload
npm i --save-dev @types/rod-fileupload   

How to Use It

  • Import the uploadSingle for uploading single file:

    import  UploadSingle  from 'rod-fileupload';
  • Import the uploadMultiple for uploading multiple file:

    import  {uploadMultiple}  from 'rod-fileupload';
  1. Use uploadSingle or uploadMultiple as middleware in your routes:

    app.post('/upload', uploadSingle('file', cloudinaryConfig), (req, res) => {
      // Your controller coded goes here
    });
    app.post('/upload', uploadMultiple('file', cloudinaryConfig), (req, res) => {
      // Your controller coded goes here
    });
  2. both uploadSingle and uploadMultiple receives two parameters:

    • fieldName (string): The name of the property containing the file information that will be added to req.body as name of property containing file(s) informations. This must be a string and this must be the same as the name of field from client that is sent with file to be uploaded

    • cloudinaryConfig (CloudinaryConfig): An object containing your Cloudinary credentials, which must be structured as follows:

      interface CloudinaryConfig {
        cloudName: string;
        apiKey: string;
        apiSecret: string;
        folder?: string;
      }

      These credentials are provided by Cloudinary.

How to Access File Information

After a file(s) is successfully uploaded, the middleware will add an object named after the fieldName (the first parameter of the middleware) to req.body. NB: advised to use string with no spaces for this fieldName

  • for upload uploadSingle:

The req.body[fieldName] will be an object and will look like this:

{
  "url": "https://res.cloudinary.com/your-cloud-name/image/upload/v1637040156/example.png",
  "public_id": "example_public_id",
  "format": "png",
  "size": 123456,
  "name": "example",
  "type": "image",
  "time": "2023-03-12T10:00:00Z"
}
  • for upload uploadMultiple:

The req.body[fieldName] will be an array of objects and will look like this:

[
  {
  "url": "https://res.cloudinary.com/your-cloud-name/image/upload/v1637040156/example.png",
  "public_id": "example_public_id",
  "format": "png",
  "size": 123456,
  "name": "example",
  "type": "image",
  "time": "2023-03-12T10:00:00Z"
},
{
  "url": "https://res.cloudinary.com/your-cloud-name/image/upload/v1637040156/example.png",
  "public_id": "example_public_id",
  "format": "png",
  "size": 123456,
  "name": "example",
  "type": "image",
  "time": "2023-03-12T10:00:00Z"
}
]

Here’s a breakdown of the fields:

  • url (string): The secure URL provided by Cloudinary for accessing the file.
  • public_id (string): The unique Cloudinary ID of the uploaded file, used to reference the file.
  • format (string): The file format (e.g., .png, .jpg, .pdf).
  • size (number): The file size in bytes.
  • name (string): The name of the uploaded file excluding the extension (e.g., example from example.png).
  • type (string): The type of the uploaded file (e.g., image, video).
  • time (string): The timestamp when the file was uploaded.

Error Handling and Status Codes

In case of failure, the middleware will respond with the appropriate HTTP status code and an error message:

  1. If the file upload fails (e.g., Multer error or Cloudinary upload error):

    • Status Code: 400 (Bad Request)
    • Message: "Error uploading file(s)"
  2. If no file is provided in the request:

    • Status Code: 400 (Bad Request)
    • Message: "No file(s) uploaded"
  3. If fieldName is not match with fieldName in the request:

  • Status Code: 400 (Bad Request)
  • Message: "Error uploading file(s)"
  • error: "Unexpected field"
  1. If there’s an error while uploading to Cloudinary:
    • Status Code: 500 (Internal Server Error)
    • Message: "Error uploading to Cloudinary"

Example of error handling:

app.post('/upload', uploadSingle('file', cloudinaryConfig), (req:Request, res:Response) => {
  try {
    // Logic to process the uploaded file
    
    res.status(200).json({message:'File uploaded successfully', file_info:req.body['file']}); // to acces the file info we use bracket notation to avoid error in case the first paramater of our middleware is word with spaces like 'my file'  (eve if is it is advised to use string without space)
  } catch (error:any) {
    res.status(500).json({ message: 'Error uploading file', error: error.message });
  }
});