0.3.1 • Published 3 years ago

file-api-mongodb v0.3.1

Weekly downloads
5
License
MIT
Repository
github
Last release
3 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

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.4

5 years ago

0.2.2

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.17

5 years ago

0.0.18

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago