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
Storing Files on Disk vs Memory in Express
📖 Scenario: You are building a simple Express server that accepts file uploads. You want to learn how to store uploaded files either directly on the server's disk or temporarily in memory.This is useful when you want to process files quickly without saving them permanently, or when you want to save files for later use.
🎯 Goal: Create an Express server with two routes: one that saves uploaded files to disk, and another that stores files in memory. You will configure the file upload middleware accordingly and handle the uploaded files.
📋 What You'll Learn
Create an Express app with the express package
Use multer middleware for file uploads
Configure multer to store files on disk in a folder named uploads
Configure multer to store files in memory as buffers
Create two POST routes: /upload/disk and /upload/memory
Handle the uploaded file in each route and respond with the file's original name
💡 Why This Matters
🌍 Real World
Uploading files is common in web apps for user avatars, documents, or images. Knowing how to store files on disk or in memory helps optimize performance and storage.
💼 Career
Backend developers often handle file uploads. Understanding multer and Express routes is essential for building APIs that accept and process files.
Progress0 / 4 steps
1
Set up Express app and import multer
Create an Express app by importing express and multer. Then initialize the app with const app = express().
Express
Hint
Use require('express') and require('multer') to import the packages.
2
Configure multer to store files on disk
Create a multer storage configuration called storageDisk that saves files to the uploads folder on disk using multer.diskStorage(). Then create an upload middleware called uploadDisk using multer({ storage: storageDisk }).
Express
Hint
Use multer.diskStorage() with destination and filename functions.
3
Configure multer to store files in memory
Create a multer upload middleware called uploadMemory that stores files in memory by setting storage to multer.memoryStorage().
Express
Hint
Use multer.memoryStorage() inside the multer() call.
4
Create routes to handle file uploads
Create two POST routes on the Express app: /upload/disk and /upload/memory. Use uploadDisk.single('file') middleware for the disk route and uploadMemory.single('file') middleware for the memory route. In each route handler, respond with JSON containing the uploaded file's original name using res.json({ filename: req.file.originalname }).
Express
Hint
Use app.post with the correct route paths and multer middleware. Access the uploaded file with req.file.originalname.
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
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 B
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
Step 1: Identify multer memory storage syntax
Memory storage is set by calling multer.memoryStorage() and passing it to the storage option.
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
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 D
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: