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-file-backet

v1.0.0

Published

This is a js backend library for express used to upload files quickly. It uses express-fileupload to do it's job, and it auto renames the file to prevent issues that comes with dublicate file name

Downloads

4

Readme

express-file-backet

This is a js backend library for express used to upload files quickly. It uses express-fileupload to do it's job, and it auto renames the file to prevent issues that comes with dublicate file name

To install, use npm i express-file-backet and npm i express-fileupload together

Usage

Set your express app

const express = require('express')
const expressFileupload = require("express-fileupload");
const {saveFiles, deleteFiles, getFiles} = require("express-file-backet");

const app = express()

const port = 3000 //port

//add middleware
app.use(expressFileupload())

app.post('/upload', async (req, res) => {
    /*if you have named your file as file you can get it as below 
    or if the input field picking the file is having name attribute 
    set to file
    */
    const file = req.files.file
    const uploadedFile =await seveFiles(file)
    
    return res.send(uploadedFile)
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

The above code is enough to save the file .Also remember to fix things like cors and others or you will have an error

The method saveFile saves both single and multiple files passed to it and returns an array of string of the files' new names it has uploaded successefully.

By default it will save the files in the directory 'public/uploads', if the directory does not exist it will create it.

It also takes in an optional second parameter which is an object

const file = req.files.file
const uploadedFile = await seveFiles(file, {
    upload_path:"uploads/images", 
    allowed_ext:['jpg', 'png', 'JPEG'],
    min_size:10,
    max_size:1000,
    })

upload_path

This is your prefered path where you want the file to be uploaded to, in the above case it will upload to uploads/images not in public/uploads, if the directory does not exist it will create it.

allowed_ext

This is an array of strings of the type of file that you want it to allow to be upload, in the above case it will save only images which has extensions 'jpg', 'png', 'JPEG' and 'jpeg'. If you pass ['jpeg', 'JPEG'] it will be ['jpeg'], so no need to repeat jpeg and JPEG.

min_size

Is the minimum size (kbs) of file you want to allow uploading.

max_size

Is the maximum size (kbs) of file you want to allow uploading.

Express-file-backet also has other methods like

deleteFiles

This method is use for deleting files which has already been uploaded. It takes in two mandatory parameter reterns null for successful deleting.

app.delete('/delete', (req, res) => {
    const uploadedFile = deleteFiles('public/uploads', 'my_image.jpg')
    
    return res.send('deleted')
})

The above code deletes a file named my_image.jpg from a directory public/uploads. The second parameter can also be an array of file names as below.

app.delete('/delete', (req, res) => {
    const uploadedFile = deleteFiles('public/uploads', ['my_image.jpg','test.mp4', 'notes.docx'])
    
    return res.send('deleted')
})

getFiles

This is a method use to read the file that you want from the upload path, it returns an array of string of full file path including the base url of the server.

app.get('/images', (req, res) => {
    const image = getFiles(req, 'uploads', 'my_image.jpg')
    
    return res.send({image})
})

The first parameter is the req object, the second is the upload inside your statict directiry and the third is the file name. You can also pass the third parameter as an array of file names like ['logo.png' , 'music.mp3']. Don't forget to set the static path to folder your are uploading to check the express documentation for how to set static path.

app.use(express.static('public'))

If you don't want to include the baseurl of the server in the image path, pass the first parameter as null.

Theanks from express-file-backet.