0.3.1 • Published 2 years ago

file-api-mongodb v0.3.1

Weekly downloads
5
License
MIT
Repository
github
Last release
2 years ago

publish npm package publish npm package

file-api-mongodb

Store files in MongoDb accessed through an express middleware.

const {multerUpload} = require("file-api-mongodb");

const {upload, storage } = multerUpload({bucketName : 'darbyBucket'});
// It's very crucial that the file name matches the name attribute in your html
appRouter.post('/', upload.single('myFile'),
    (req, res) => {
    //the successful result of the file upload is in "fileData" field.
    res.json(req.fileData);
});

appRouter.get('/', (req,res) => {
    storage.getFileList(res);
});

appRouter.get('/:fileName', (req,res) => {
    storage.getFile(res, req.params.fileName);
});

appRouter.get('/id/:id', (req,res) => {
    storage.getFileById(res, req.params.id);
});

appRouter.delete('/:id', (req,res) => {
    storage.deleteFile(req.params.id, (err) => {
        if(err)
            res.status(500).send(err.message);
        else
            res.status(204).send();
    });
});

To scale an image that is being uploaded, set the request parameter "imageWidth"

appRouter.post('/image/:imageWidth', upload.single('myFile'),
    (req, res) => {
        res.json(req.fileData);
    });

Options

Two options are available for the constructor: | variable | description | | :---: | :--- | |connectDb (optional) | function that returns db connection. If a connectDb function is not provider, the variable "process.env.DB_CONN" is used mongoClient.connect(process.env.DB_CONN)| |bucketName (optional)| the default value is "fileBucket"| |dbName (optional)| the default value is "identity" is the database name|

The storage object was derived from the following template

StorageEngine

0.3.1

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.4

3 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago