3

I have an Express 4 application which needs to process data files uploaded by users and return an output. The files are relatively small (1-2 MB), so I'd like to upload them directly to memory (e.g. as a UTF-8 string variable) rather than saving them to disk.

The closest I've gotten so far is the multer module and its onFileUploadData event, but I can't figure out how to take the data and pass it to a route handler in a separate file.

Is this possible with Node.js / Express?

user456584
  • 86,427
  • 15
  • 75
  • 107

1 Answers1

7

I don't know if this option was available at the time of the question, but it certainly is now. You can set the inMemory option to true in multer and that will give you a buffer object. Likes this:

app.use(multer({
    dest: './uploads/',
    inMemory: true
}));

You can find all the available options for multer here: https://github.com/expressjs/multer#options


Update for multer 1.x

Multer version 1.0.0 introduces the concept of storage engines and ships with two default engines: DiskStorage and MemoryStorage.

This is how you can instantiate it to store the files in memory:

multer({ storage: multer.memoryStorage({}) })
Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
earthling
  • 555
  • 4
  • 14