Coder Social home page Coder Social logo

chenshuaicheng / express-formidable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hatashiro/express-formidable

0.0 1.0 0.0 198 KB

An Express middleware of Formidable that just works.

Home Page: https://github.com/noraesae/express-formidable

License: MIT License

JavaScript 100.00%

express-formidable's Introduction

express-formidable Build Status

An Express middleware of Formidable that just works.

What are Express, Formidable, and this?

Express is a fast, unopinionated, minimalist web framework for Node.js.

Formidable is a Node.js module for parsing form data, including multipart/form-data file upload.

So, express-formidable is something like a bridge between them, specifically an Express middleware implementation of Formidable.

It aims to just work.

Install

npm install express-formidable

How to use

const express = require('express');
const formidableMiddleware = require('express-formidable');

var app = express();

app.use(formidableMiddleware());

app.post('/upload', (req, res) => {
  req.fields; // contains non-file fields
  req.files; // contains files
});

And that's it.

express-formidable can basically parse form types Formidable can handle, including application/x-www-form-urlencoded, application/json, and multipart/form-data.

Option

app.use(formidableMiddleware(opts));

opts are options which can be set to form in Formidable. For example:

app.use(formidableMiddleware({
  encoding: 'utf-8',
  uploadDir: '/my/dir',
  multiples: true, // req.files to be arrays of files
});

For the detail, please refer to the Formidable API.

Events

app.use(formidableMiddleware(opts, events));

events is an array of json with two field:

Field Description
event The event emitted by the form of formidable. A complete list of all the possible events, please refer to the Formidable Events
action The callback to execute. The signature is function (req, res, next, ...formidable_parameters)

For example:

const events = [
  {
    event: 'fileBegin',
    action: function (req, res, next, name, file) { /* your callback */ }
  }, 
  {
    event: 'field',
    action: function (req, res, next, name, value) { /* your callback */ }
  }
];

Error event

Unless an error event are provided by the events array parameter, it will handle by the standard next(error).

Contribute

git clone https://github.com/utatti/express-formidable.git
cd express-formidable
npm install

To lint and test:

npm test

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.