Koa.js Multer Library Tutorial to Upload Single or Multiple Files Example Using Koa-Multer Module Full Example For Beginners


Welcome folks today in this blog post we will be uploading files (single or multiple) in koa.js using multer library .All the full source code of the application is shown below.




Get Started




In order to get started you need to install the below library using the npm command as shown below



npm i @koa/multer multer



After installing this library make a index.js file and copy paste the following code






const Koa = require('koa');
const Router = require('@koa/router');
const multer = require('@koa/multer');

const app = new Koa();
const router = new Router();
const upload = multer(); // note you can pass `multer` options here

// add a route for uploading multiple files
      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 files
  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 app

// start the server

Leave a Reply