Code Monkey home page Code Monkey logo

bodyreceiver's Introduction

bodyreceiver

A request body receiver & parser middleware for Koa@2+.

Support body type:

  • application/json
  • application/x-www-form-urlencoded
  • text/plain
  • multipart/form-data

Install

// npm
npm install bodyreceiver
// yarn
yarn add bodyreceiver

Usage

var Koa = require('koa');
var BodyReceiver = require('bodyreceiver');

var app = new Koa();
var bodyReceiver = new BodyReceiver();

app.use(bodyReceiver.startup());

app.use((ctx, next) => {
	// Received body pass to ctx.request.body
	ctx.body = ctx.request.body;
	return next();
});

app.listen(3000, '127.0.0.1');

API

  • BodyReceiver([options])

BodyReceiver main Class, will return new instance.

var bodyReceiver = new BodyReceiver();
  • bodyReceiver.startup()

Register bodyReceiver middleware for Koa.

Upload file

<!-- Example form html -->
<form action="/file" method="POST" enctype="multipart/form-data">
    <input type="text" name="firstname" />
    <input type="text" name="lastname" />
    <input type="file" name="image" />
    <button>Submit</button>
</form>
// upload file result
console.log(ctx.request.body);

{
    fields: { // other filed
        firstname: 'foo',
        lastname: 'bar',
    },
    files: {
        image: [{
            name: 'b84f4a4a275002d3379c3be782369cff.jpg', // filename
            type: 'image/jpeg', // filetype
            size: 976, // filesize
            contents: <Buffer ...>,  // file contents is Buffer type
            createReadStream: [Function]  // readable Stream
        }]
    }
}

If use FileReader read base64 file in browser, then upload the base64 file, set the content-type header to text/plain.

Options

  • accept Regexp / Function, Upload file only, default is null, means accept all file type.

limit accept file type, if accept is Regexp, will test file MIME type, if accept is Function, will invoke function test file type and size.

{
    accept: function (type, size) {
        // must return Boolean result
    }
}
  • write Boolean, Upload file only, default is false.

Whether write file to disk, uploaded file contents default is Buffer type, can pass through the Stream transmission.

  • maxBodySize String, default is 1mb.

Maximum body size, the value can pass through bytes parse, if body size greater than maxBodySize will emit error event.

  • maxFileSize String, Upload file only, default is 3mb.

Maximum uploaded file size, the value can pass through bytes parse, if file size greater than maxFileSize will emit error event.

  • minFileSize String, Upload file only, default is null.

Minimum uploaded file size, the value can pass through bytes parse, if file size less than minFileSize will emit error event.

  • keepFilename Boolean, Upload file only, default is false.

Whether keep origin file name. If value is false, the BodyReceiver will generate a new hash name by file contents, is value is true, keep origin file name.

  • generateFilename Boolean, Upload file only, default is null.

Custom generate file name rules.

{
    generateFilename: function (originName, hashName, extName) {
        // must return a new filename
    }
}
  • dest String, Upload file only, default is process.cwd() + '/upload'.

Uploaded file save folder, the dest must coordination write usage, if dest folder is not exists, the BodyReceiver will create a new folder, the new folder permission is 0777.

Events

The BodyReceiver can emit various events.

  • error Error event, body parse error, file type or size is invalid, both emit error event.
bodyReceiver.on('error', function (error, ctx) {
    console.error(error);
    ctx.throw(error, 422);
});
  • progress Receive file progress event.

  • aborted Receive file aborted event.

  • data If the file is valid, will emit data event.

  • file If the file is write to disk, will emit file event.

  • end If the file receive is end, will emit end event.

License

MIT @ Yiguo Chen

bodyreceiver's People

Contributors

chenmnkken avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.