1.0.3 • Published 2 years ago

mongofiles v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

mongofiles

A package to easily save filenames to MongoDB

Installation

Install mongofiles with npm

  npm install mongofiles

Tech Stack

Server: Node, Express

Usage/Examples

const Blog = require("../models/blog");
const mongofiles = require("mongofiles"); //importing mongofiles

exports.add = (req, res, next) => {
  var {
    images,
    featuredimage
  } = req.body;  

//for more than one files, it return array of filenames
  images = mongofiles.manyfile( req.files.images,  "images", "filename"  );
 
//for only one file it returns a string  
  featuredimage = mongofiles.onefile(req.files.featuredimage, featuredimage, "filename");

  let newBlog = new Blog({
    images,
    featuredimage
  });

  newBlog.save((err, data) => {
    if (err) {
      return res.status(400).json({
        error: err,
      });
      console.log(err);
    } else {
      res.json(data);
      console.log(data);
    }
  });
};

API Reference

//return a array of filenames
  mongofiles.manyfiles(req.files.<name>, "name" , "Option", custom )

//return a string of single file
  mongofiles.onefile(req.files.<name>, "name" , "Option", custom ) 

Select any for options Required Options are same for manyfiles and onefile | Option | Type | Description | | :-------- | :------- | :------------------------- | | original | string | Returns original filename | | filename | string | Returns filename uploaded with the name | | customfilename | string | Returns filename uploaded file with the custom string or number name | | customoriginalname | string | Returns original uploaded file with the custom string or number name |

Custom is only mandatory if you go for customfilename and customoriginalname

You can write string or number there or pass Date.now()

Authors