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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-fileforge

v1.8.3

Published

`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

Downloads

170

Readme

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.