You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Route middleware for Koa that handles multipart/form-data using multer
Call for Maintainers
This module is a fork of koa-multer, the most widely used multer middleware in the koa community. Due to lack of maintenance, it was forked to the official Koa organization and is available under @koa/multer package name.
Install
Note that you must install either multer@1.x (Buffer) or multer@2.x (Streams):
npm install --save @koa/multer multer
Usage
constKoa=require('koa');constRouter=require('@koa/router');constmulter=require('@koa/multer');constapp=newKoa();constrouter=newRouter();constupload=multer();// note you can pass `multer` options here// add a route for uploading multiple filesrouter.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';});// add a route for uploading single filesrouter.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';});// add the router to our appapp.use(router.routes());app.use(router.allowedMethods());// start the serverapp.listen(3000);