1.0.2 • Published 7 months ago

dizo v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

dizo

dizo provides a middleware used for handling multipart/form-data.

Remember : It is build only to proccess multipart/form-data. So make sure to add enctype attribute to your form tag, whose value must be "multipart/form-data".

Features

  • If a file already exists or have the same name in the storage, dizo will never overwrite that file.
  • Having mutliple file keys attached to formData, dizo can proccess it all.
  • dizo is not dependent on any third party libraries/packages, which makes it a lightweight package.

Installation

$ npm install dizo

Example

const express = require("express");
const dizo = require("dizo");

const app = express();

const config = {
    bodyFetch : ["name", "age"],
    fileFetch : ["profile"],
    saveFiles : true,
    saveFilesAt : `${__dirname}/container`,
    returnBuffer : false,
    validate : (body, fileContainer) => {
        /**
         * 'body' contains all the non-file data.
         * 'fileContainer' contains all the file related data.
         * 
         * if(true)
         *    return [true]
         * else
         *    return [false, errorDetails] 
        **/

        return [true];
    }
};

app.post("/files", dizo(config), cb);

function cb(req, res) {
  res.json({ body : req.body, fileContainer : req.fileContainer });
}

app.listen(1234, (error) => console.log(error || "Server has started...!"));

'config' variable information

FieldTypeDefaultDescription
1bodyFetchstring[]*It's an optional field. It will only fetch those values which are present in the formData. In our example, 'name' and 'age' non-file keys will be fetched from the formData. All non-file keys will be fetched, if provided null, undefined or '*'.
2fileFetchstring[]*It's an optional field. It will only fetch those values which are present in the formData. In our example, 'profile' file key will be fetched from the formData. All file related keys will be fetched, if provided null, undefined or '*'.
3saveFilesbooleanfalseIt's an optional field. Files will be saved in the storage if the value of this field is true.
4saveFilesAtstring""It's an optional field. It is the path of the directory in which the files will be saved. Please note, in order to save files, the value of 'saveFiles' must be true.
5returnBufferbooleanfalseIt's an optional field. If true, every object in req.fileContainer will contain a field buffer whose value will be the bufferData of that particular file.
6validateFunction() => trueIt's an optional field. In this function, we can apply our custom validations to each & every key in formData. If the formData is valid return true or else return false, error.

A message from the author

For new users, start by printing the value of req.body & req.fileContainer in your standard o/p device for better understanding of this package.

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago