Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-fileforge

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-fileforge

`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 you

  • 1.8.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
40
decreased by-54.55%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 06 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc