express-file-backet
v1.0.3
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.