npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bodyreceiver

v1.1.0

Published

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

Downloads

4

Readme

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