Route middleware for Koa that handles multipart/form-data
using multer in authentic ESM.
This module is a ESM fork of @koa/multer which is a fork of koa-multer, the most widely used multer middleware in the koa community.
Install
Note that you must install either multer@1.x
(Buffer) or multer@2.x
(Streams):
npm install --save koa-multer-esm multer
Usage
import Koa from "koa";
import Router from "@koa/router";
import multer from "koa-multer-esm";
const app = new Koa();
const router = new Router();
const upload = multer();
router.post(
'/upload-multiple-files',
upload.fields([
{
name: 'avatar',
maxCount: 1
},
{
name: 'boop',
maxCount: 2
}
]),
ctx => {
console.log('ctx.request.files', ctx.request.files);
console.log('ctx.files', ctx.files);
console.log('ctx.request.body', ctx.request.body);
ctx.body = 'done';
}
);
router.post(
'/upload-single-file',
upload.single('avatar'),
ctx => {
console.log('ctx.request.file', ctx.request.file);
console.log('ctx.file', ctx.file);
console.log('ctx.request.body', ctx.request.body);
ctx.body = 'done';
}
);
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
Contributors
License
MIT © Fangdun Cai