Multer: Clarification: Using one instance in multiple routes

Created on 19 Sep 2019  路  2Comments  路  Source: expressjs/multer

Out of curiosity: Is it okay to share a single Multer instance between several routes to save duplicated setup code? Something like:

import * as multer from 'multer';

const multerInstance = multer({ 
  fileFilter: myFilter, 
  limits: { 
    fileSize: 1024 * 1024 
  } 
}).single('file');

app.post('/route1', multerInstance, handler1);
app.post('/route2', multerInstance, handler2);
app.post('/route3', multerInstance, handler3);

Or would this bring any side-effects and I should create separate instances instead?

Most helpful comment

@qqilihq Creating multerInstance object once and re-using it as middleware function to different routes/clients doesn't affect in my opinion because,

  1. Definition of multer is constant for all routes ( Ex: file filter, file limits, form identifier,..)
  2. Each request/route starts a new middleware function, so are completely independent
    of each other (Ex: busboy, ..)

All 2 comments

@qqilihq Creating multerInstance object once and re-using it as middleware function to different routes/clients doesn't affect in my opinion because,

  1. Definition of multer is constant for all routes ( Ex: file filter, file limits, form identifier,..)
  2. Each request/route starts a new middleware function, so are completely independent
    of each other (Ex: busboy, ..)

@HarshithaKP Thanks, that sounds very reasonable!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thammarith picture thammarith  路  3Comments

BlueOctober picture BlueOctober  路  3Comments

trexanhvn picture trexanhvn  路  3Comments

sant123 picture sant123  路  4Comments

Paul-Morris picture Paul-Morris  路  3Comments