0.4.4 • Published 5 years ago

@robotmayo/kmultipart v0.4.4

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

kmultipart

A multipart body parser for koa. Definitely needs more eyes and testing before use in production.

Install

npm i @robotmayo/kmultipart

Features

  • Custom storage engine

Quick Start

import KRouter from "@robotmayo/krouter";
import multipart, { Diskstorage } from "@robotmayo/kmultipart";
import * as Koa from "koa";
const app = new Koa();
const router = new KRouter();

router.post(
  "/upload",
  multipart({ storage: new DiskStorage({ destination: "/files" }) }),
  (ctx, next) => {
    ctx.body = "Uploaded ${ctx.request.files.length} file(s) succesfully";
  }
);

app.use(router.middleware());
app.listen();

Api

multipart(opts) Returns middleware that parses multipart forms using busboy

ParameterRequiredTypeDescription
optsTrueobject
storageEngineTrueStorageEngineA storage engine object or class instance

Kmultipart requires a storage engine to function. It comes with two built in ones but its very simple to create your own. A custom storage engine is simple an object or class instance with the function handleFile. It takes a single object containing the file stream and other information.

{handleFile(handleFilePart)}

handleFilePart

ParameterTypeDescription
fileNodeJS.ReadableStreamThe file stream
filenamestringthe original filename as it appeared on their computer
fieldnamestringthe fieldname of the object
ctxKoa.Contextthe koa context
encodingstringfile encoding
mimetypestringfile mimetype

Why?

The major existing solutions dont support custom storage engines. Also writing my own sounded fun.