1.1.1 • Published 5 months ago

rod-fileupload v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

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.bodyfieldName 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.bodyfieldName 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 });
  }
});
1.1.1

5 months ago

1.1.0

5 months ago

1.0.43

5 months ago

1.0.42

5 months ago

1.0.41

5 months ago

1.0.4

5 months ago

1.0.33

5 months ago

1.0.32

5 months ago

1.0.31

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago