Git Product home page Git Product logo

xoventechdev / express-fileforge Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rupomsoft/express-fileforge

1.0 0.0 0.0 195 KB

express-fileforge is a file upload utility for Express.js that simplifies file handling. It provides a convenient way to save files to the server and manage file storage paths. This package is designed to be easy to use and integrate seamlessly with your Express.js applications.

License: MIT License

JavaScript 100.00%

express-fileforge's Introduction

express-fileforge is a file upload utility for Express.js that simplifies file handling. It provides a convenient way to save files to the server and manage file storage paths. This package is designed to be easy to use and integrate seamlessly with your Express.js applications.

npm install express-fileforge

Express JS Example

const express = require('express');
const fileForge = require('express-fileforge'); 
const path = require('path');
const app = express();

// Your route for file upload
app.post('/upload', async function (req, res) {
    try {
        // Upload file
        let uploadedFile = await fileForge.saveFile(req, path.resolve(__dirname),'myFiles', 'abc.pdf');
        res.end(`File uploaded successfully: ${uploadedFile}`);
    } catch (error) {
        console.error(error);
        res.status(500).end('Internal Server Error');
    }
});

// Route for file deletion
app.delete('/delete/:fileName', async function (req, res) {
    try {

        // File name from the URL parameter
        const fileName = req.params.fileName;

        // Delete the specified file
        const isDeleted = await fileForge.deleteFile(path.resolve(__dirname),'myFiles',  fileName);
        if (isDeleted) {
            res.end(`File deleted successfully: ${fileName}`);
        } else {
            res.status(404).end(`File not found: ${fileName}`);
        }
    } catch (error) {
        console.error(error);
        res.status(500).end('Internal Server Error');
    }
});

// Start the server
app.listen(5050, function () {
    console.log('Server Run Success');
});

How To Upload Using Fetch

async function uploadFile() {
  try {
    var formdata = new FormData();
    formdata.append("files", fileInput.files[0]);

    var requestOptions = {
      method: 'POST',
      body: formdata,
      redirect: 'follow',
    };

    const response = await fetch("http://localhost:5050/uploads", requestOptions);
    const result = await response.text();
    console.log(result);
  } catch (error) {
    console.log('error', error);
  }
}

// Call the function
uploadFile();

How To Upload Using Axios

async function uploadFile() {
    try {
        const formData = new FormData();
        formData.append("files", fileInput.files[0]);

        const response = await axios.post("http://localhost:5050/uploads", formData, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        });

        console.log(response.data);
    } catch (error) {
        console.error('Error:', error);
    }
}

// Call the function
uploadFile();

How To Delete Using Fetch

const deleteFile = async () => {
    const FileName = "abc.pdf";
    const requestOptions = {method: 'DELETE'};
    try {
        const response = await fetch(`http://localhost:5050/delete/${FileName}`, requestOptions);
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
        const result = await response.text();
        console.log(result);
    } catch (error) {
        console.log('error', error);
    }
};
// Call the async function
deleteFile();

How To Delete Using Axios

const deleteFile = async () => {
    const FileName = "abc.pdf";
    try {
        const response = await axios.delete(`http://localhost:5050/delete/${FileName}`);
        console.log(response.data);
    } catch (error) {
        console.log('error', error.message);
    }
};
// Call the async function
deleteFile();

License

This package is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • This package is inspired by the need for a simple and efficient file upload solution for Express.js applications.
  • Thanks to the Express.js and Node.js communities for their valuable contributions.

express-fileforge's People

Contributors

rupomsoft avatar

Stargazers

Jahid Iqbal Biswas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.