Express 4: How to upload file to memory (e.g. as a UTF-8 string) rather than disk?

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?

I believe multer will always write to disk. You may need to use busboy directly if you want to avoid hitting the disk.

You can then just set up a busboy instance (or use connect-busboy to help get rid of some boilerplate) in your route handler. Just make sure you don't have any middleware that reads request data before that route handler executes.

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({}) })