Module 45: Files and Logging

Lesson 2 of 5 · 15–25 minutes

File uploads

🏠The everyday picture

A post office accepts parcels, but checks the size and weight, refuses some contents, and gives each parcel its own tracking number rather than using whatever the sender wrote on it.

📘What it really is

const upload = multer({
  storage: multer.memoryStorage(),
  limits: { fileSize: 5 * 1024 * 1024, files: 1 },   // 5 MB
});

enquiriesRouter.post("/:id/attachments", requireAuth, upload.single("file"), async (req, res) => { ... });

🖱️Try it

Add an attachment upload route for enquiries using multer with a 5 MB limit. Save each file to an uploads/ folder (in .gitignore) under a new random id, never the original name, and store the original name, size and type in the enquiry's data. Add a download route that checks the person is signed in.

✅Quick check

Why save an uploaded file under a random id instead of its original name?

Show the answer

Original names can clash, contain dangerous characters or ../, and reveal information.

Why do many apps keep uploads in object storage rather than on the server's disk?

Show the answer

It's durable and shared, while a server's disk can be lost or isn't shared between servers.

← PreviousFile operations
ProLearners Assist