1.0.4-beta.2.0 • Published 5 months ago

ddivfs v1.0.4-beta.2.0

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

ddivfs

ddivfs is a high-performance, lightweight Node.js middleware designed for handling multipart/form-data, making file uploads seamless and efficient. It ensures optimal performance with minimal resource usage.

Whether you're handling small files or large-scale uploads, ddivfs is designed to be fast, reliable, and easy to integrate with your existing Express.js or Node.js applications.

🚀 Why Choose ddivfs?

✔ Blazing Fast – Optimized for speed and efficiency
✔ Minimal Memory Usage – Handles large file uploads without excessive RAM consumption
✔ Seamless Integration – Works effortlessly with Express.js and other frameworks
✔ Single File Support – Upload single files in a single request
✔ Every week new features – New features added every week
âš  Note: ddivfs only processes forms encoded as multipart/form-data. Other form types will be ignored.

📖 Documentation & More: Explore the official documentation and updates at ddiv.online.

Installation

npm install ddivfs

🚀Usage

📌 Ensure Your Form Uses multipart/form-data .To correctly upload a file, your form must use the multipart/form-data encoding type. Here's an example HTML form: .

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Upload File</title>
</head>
<body>
   <input type="file" id="fileInput" />
   <button onclick="uploadFile()">Upload</button>

   <script>
       function uploadFile() {
           const fileInput = document.getElementById("fileInput");
           const file = fileInput.files[0];

           const formData = new FormData();
           formData.append("file", file);

           fetch("http://localhost:5000/upload", {
               method: "POST",
               body: formData,
           })
           .then(response => response.json())
           .then(data => console.log("Success:", data))
           .catch(error => console.error("Error:", error));
       }
   </script>
</body>
</html>

📌 Handling File Uploads in Node.js with ddivfs

const express = require("express");
const ddivfs = require("ddivfs");

const app = express();

app.post("/file", async (req, res) => {
   let arrayBuffer = [];

   req.on("data", (chunk) => {
      arrayBuffer.push(chunk);
   });

   req.on("end", async () => {
      let buffer = Buffer.concat(arrayBuffer);
      
      // Create an instance of the fileStream class with the request and buffer
      let sendFl = new ddivfs(req, buffer, 'me');
      
      try {
         // Process the file and get the information
         let file = await sendFl.buffersend();
         console.log(file);
         //if you want to see the information about the file 
         let fileInfo = await sendFl.getFileInfo();
         console.log(fileInfo);
         
         // Send the processed file as a response
         res.json(file);
      } catch (error) {
         res.status(500).json({ error: 'File processing failed' });
      }
   });
});

Collaborators