Bird
Raised Fist0
Expressframework~20 mins

Storing files on disk vs memory in Express - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
File Storage Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding file storage behavior in Express with multer
Consider an Express app using multer middleware to handle file uploads. If multer is configured with storage: multer.memoryStorage(), what happens to the uploaded file data?
Express
const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });

app.post('/upload', upload.single('file'), (req, res) => {
  console.log(req.file.buffer);
  res.send('File received');
});
AThe file is streamed directly to a cloud storage service without local storage.
BThe file is saved directly to disk in a temporary folder and req.file.path contains the file path.
CThe file is stored temporarily in RAM as a Buffer accessible via req.file.buffer.
DThe file is discarded immediately and not accessible in the request handler.
Attempts:
2 left
💡 Hint
Think about where memoryStorage keeps the file data.
component_behavior
intermediate
2:00remaining
Behavior of multer.diskStorage in Express
When multer is configured with storage: multer.diskStorage(), what is true about the uploaded files?
Express
const multer = require('multer');
const storage = multer.diskStorage({
  destination: (req, file, cb) => cb(null, './uploads'),
  filename: (req, file, cb) => cb(null, file.originalname)
});
const upload = multer({ storage });

app.post('/upload', upload.single('file'), (req, res) => {
  console.log(req.file.path);
  res.send('File saved to disk');
});
AThe file is converted to a base64 string and stored in req.file.base64.
BThe file is stored in memory and req.file.buffer contains the data.
CThe file is uploaded but not saved anywhere automatically.
DThe file is saved to the specified disk folder and req.file.path contains the file path.
Attempts:
2 left
💡 Hint
Disk storage means saving files on your server's hard drive.
📝 Syntax
advanced
2:00remaining
Identifying error in multer memoryStorage usage
Which option will cause a runtime error when trying to access the uploaded file's data in an Express route using multer with memoryStorage?
Express
const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });

app.post('/upload', upload.single('file'), (req, res) => {
  // Access file data here
});
Aconsole.log(req.file.path.length);
Bconsole.log(req.file.buffer.toString('utf8'));
Cconsole.log(req.file.size);
Dconsole.log(req.file.mimetype);
Attempts:
2 left
💡 Hint
memoryStorage does not save files on disk, so some properties are missing.
state_output
advanced
2:00remaining
Comparing memory usage between diskStorage and memoryStorage
In an Express app handling large file uploads, what is a key difference in server memory usage between multer's diskStorage and memoryStorage?
ABoth use the same amount of RAM because multer buffers files in memory regardless of storage type.
BmemoryStorage uses more RAM because files are kept in memory; diskStorage uses less RAM but uses disk space.
CdiskStorage uses more RAM because it buffers files before saving; memoryStorage writes directly to disk.
DmemoryStorage uses less RAM because it streams files directly to disk.
Attempts:
2 left
💡 Hint
Think about where the file data lives during upload.
🔧 Debug
expert
2:00remaining
Debugging file upload failure with multer diskStorage
An Express app uses multer.diskStorage to save uploaded files to './uploads'. However, uploads fail silently and no files appear in the folder. What is the most likely cause?
AThe './uploads' folder does not exist or lacks write permissions.
BThe Express app is missing the body-parser middleware.
CThe file input field name in the HTML form does not match the multer field name.
DThe multer middleware is missing the .single() or .array() method call.
Attempts:
2 left
💡 Hint
Check if the folder exists and can be written to by the server.

Practice

(1/5)
1. In Express, what is the main difference between storing uploaded files on disk versus in memory?
easy
A. Disk storage is faster than memory storage for file uploads.
B. Disk storage saves files physically on the server, memory storage keeps files temporarily in RAM.
C. Memory storage saves files permanently, disk storage deletes files after upload.
D. Disk storage only works with images, memory storage only works with text files.

Solution

  1. Step 1: Understand disk storage in Express

    Disk storage saves uploaded files physically on the server's hard drive, making them persistent.
  2. Step 2: Understand memory storage in Express

    Memory storage keeps files temporarily in RAM, which is faster but not persistent after server restarts.
  3. Final Answer:

    Disk storage saves files physically on the server, memory storage keeps files temporarily in RAM. -> Option B
  4. Quick 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
A. const upload = multer({ storage: multer.memoryStorage() });
B. const upload = multer({ storage: multer.diskStorage({}) });
C. const upload = multer({ dest: '/uploads' });
D. const upload = multer({ storage: 'memory' });

Solution

  1. Step 1: Identify multer memory storage syntax

    Memory storage is set by calling multer.memoryStorage() and passing it to the storage option.
  2. Step 2: Check options for correct syntax

    const upload = multer({ storage: multer.memoryStorage() }); correctly uses multer.memoryStorage() inside the storage property.
  3. Final Answer:

    const upload = multer({ storage: multer.memoryStorage() }); -> Option A
  4. Quick 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:
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
A. Throws an error because buffer is not available
B. Logs undefined, sends 'File size: undefined'
C. Logs 0, sends 'File size: 0'
D. Logs 5000, sends 'File size: 5000'

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Logs 5000, sends 'File size: 5000' -> Option D
  4. Quick 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:
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
A. The destination path './uploads' does not exist or lacks write permission.
B. filename callback must return a Promise instead of using cb.
C. upload.single should be upload.array for disk storage.
D. multer.diskStorage cannot be used with Express.

Solution

  1. Step 1: Check diskStorage destination requirements

    The destination folder must exist and be writable by the server process; multer does not create folders automatically.
  2. 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.
  3. Final Answer:

    The destination path './uploads' does not exist or lacks write permission. -> Option A
  4. Quick 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
A. Use multer.diskStorage to save files on disk for persistence and later processing.
B. Use multer.memoryStorage to keep files in RAM for fast access but risk high memory use.
C. Use multer.memoryStorage but limit file size to avoid memory overflow.
D. Use multer.diskStorage with a temporary folder and delete files after processing.

Solution

  1. Step 1: Consider large file upload challenges

    Large files can consume a lot of RAM if stored in memory, risking server crashes.
  2. Step 2: Evaluate memoryStorage with file size limits

    Using memoryStorage with strict file size limits allows fast processing while preventing excessive memory use.
  3. Step 3: Compare with diskStorage options

    Disk storage is persistent but slower; temporary disk storage with deletion adds complexity and latency.
  4. Final Answer:

    Use multer.memoryStorage but limit file size to avoid memory overflow. -> Option C
  5. Quick 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