We store files either on disk or in memory to handle uploads. Disk storage saves files on your server's hard drive. Memory storage keeps files temporarily in RAM.
Storing files on disk vs memory in Express
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Express
const multer = require('multer'); // Disk storage setup const storageDisk = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uploads/'); }, filename: function (req, file, cb) { cb(null, file.originalname); } }); // Memory storage setup const storageMemory = multer.memoryStorage(); const uploadDisk = multer({ storage: storageDisk }); const uploadMemory = multer({ storage: storageMemory });
Disk storage saves files physically on your server.
Memory storage keeps files in RAM as buffers, temporary and fast.
Examples
Express
app.post('/upload-disk', uploadDisk.single('file'), (req, res) => { res.send('File saved on disk: ' + req.file.path); });
Express
app.post('/upload-memory', uploadMemory.single('file'), (req, res) => { const fileBuffer = req.file.buffer; res.send('File received in memory, size: ' + fileBuffer.length + ' bytes'); });
Sample Program
This Express app has two routes: one saves uploaded files to disk, the other keeps them in memory. You can test by sending files to each route.
Express
const express = require('express'); const multer = require('multer'); const app = express(); // Disk storage setup const storageDisk = multer.diskStorage({ destination: (req, file, cb) => cb(null, 'uploads/'), filename: (req, file, cb) => cb(null, file.originalname) }); // Memory storage setup const storageMemory = multer.memoryStorage(); const uploadDisk = multer({ storage: storageDisk }); const uploadMemory = multer({ storage: storageMemory }); app.post('/upload-disk', uploadDisk.single('file'), (req, res) => { res.send('File saved on disk: ' + req.file.path); }); app.post('/upload-memory', uploadMemory.single('file'), (req, res) => { const size = req.file.buffer.length; res.send('File received in memory, size: ' + size + ' bytes'); }); app.listen(3000, () => console.log('Server running on http://localhost:3000'));
Important Notes
Disk storage is better for large files or permanent saving.
Memory storage is faster but uses RAM and is temporary.
Always check file size limits to avoid memory overload.
Summary
Disk storage saves files physically on your server.
Memory storage keeps files temporarily in RAM for quick use.
Choose storage based on file size, speed, and persistence needs.
Practice
1. In Express, what is the main difference between storing uploaded files on disk versus in memory?
easy
Solution
Step 1: Understand disk storage in Express
Disk storage saves uploaded files physically on the server's hard drive, making them persistent.Step 2: Understand memory storage in Express
Memory storage keeps files temporarily in RAM, which is faster but not persistent after server restarts.Final Answer:
Disk storage saves files physically on the server, memory storage keeps files temporarily in RAM. -> Option BQuick Check:
Disk = physical, Memory = temporary RAM [OK]
Hint: Disk = saved on server, Memory = temporary RAM [OK]
Common Mistakes:
- Thinking memory storage saves files permanently
- Assuming disk storage is always faster
- Confusing file types with storage methods
2. Which of the following is the correct way to configure multer for storing files in memory in Express?
easy
Solution
Step 1: Identify multer memory storage syntax
Memory storage is set by calling multer.memoryStorage() and passing it to the storage option.Step 2: Check options for correct syntax
const upload = multer({ storage: multer.memoryStorage() }); correctly uses multer.memoryStorage() inside the storage property.Final Answer:
const upload = multer({ storage: multer.memoryStorage() }); -> Option AQuick Check:
Use multer.memoryStorage() for memory storage [OK]
Hint: Use multer.memoryStorage() to store files in RAM [OK]
Common Mistakes:
- Using string 'memory' instead of multer.memoryStorage()
- Confusing diskStorage with memoryStorage
- Setting dest property for memory storage
3. Given this Express code snippet using multer with memory storage:
What will be logged and sent if a 5000-byte file is uploaded?
const upload = multer({ storage: multer.memoryStorage() });
app.post('/upload', upload.single('file'), (req, res) => {
console.log(req.file.buffer.length);
res.send('File size: ' + req.file.size);
});What will be logged and sent if a 5000-byte file is uploaded?
medium
Solution
Step 1: Understand multer memoryStorage behavior
When using memoryStorage, the uploaded file is stored in req.file.buffer as a Buffer object containing the file data.Step 2: Check properties used in code
req.file.buffer.length gives the byte length of the file buffer, which will be 5000 for a 5000-byte file. req.file.size also holds the file size in bytes.Final Answer:
Logs 5000, sends 'File size: 5000' -> Option DQuick Check:
Memory storage buffer length = file size [OK]
Hint: Memory storage files have buffer and size properties [OK]
Common Mistakes:
- Assuming buffer is undefined in memoryStorage
- Confusing size with buffer length
- Expecting disk path properties in memory storage
4. You wrote this Express code to store files on disk:
But files are not saved and no error appears. What is the likely problem?
const upload = multer({ storage: multer.diskStorage({
destination: './uploads',
filename: (req, file, cb) => cb(null, file.originalname)
}) });
app.post('/upload', upload.single('file'), (req, res) => {
res.send('File saved');
});But files are not saved and no error appears. What is the likely problem?
medium
Solution
Step 1: Check diskStorage destination requirements
The destination folder must exist and be writable by the server process; multer does not create folders automatically.Step 2: Analyze why files are not saved
If the './uploads' folder is missing or permission denied, multer silently fails to save files without throwing errors.Final Answer:
The destination path './uploads' does not exist or lacks write permission. -> Option AQuick Check:
Disk storage needs existing writable folder [OK]
Hint: Ensure upload folder exists and is writable [OK]
Common Mistakes:
- Expecting multer to create upload folders automatically
- Misusing filename callback with Promises
- Confusing single vs array upload methods
5. You want to upload large files in Express and process them quickly without saving to disk. Which approach is best and why?
hard
Solution
Step 1: Consider large file upload challenges
Large files can consume a lot of RAM if stored in memory, risking server crashes.Step 2: Evaluate memoryStorage with file size limits
Using memoryStorage with strict file size limits allows fast processing while preventing excessive memory use.Step 3: Compare with diskStorage options
Disk storage is persistent but slower; temporary disk storage with deletion adds complexity and latency.Final Answer:
Use multer.memoryStorage but limit file size to avoid memory overflow. -> Option CQuick Check:
MemoryStorage + size limit = fast and safe [OK]
Hint: Limit file size when using memory storage for large files [OK]
Common Mistakes:
- Ignoring memory limits causing crashes
- Assuming disk storage is always faster
- Not cleaning up temporary disk files
