Node.js › File Uploads & Media Processing
Why should an application typically avoid storing uploaded files directly on the same server instance that runs the web application?
BeginnerAnswer
Storing uploaded files on local disk ties that data to a specific server instance, which breaks horizontal scaling (a file uploaded to one instance wouldn't be visible from requests handled by a different instance) and risks permanent data loss if that specific instance is terminated (common in cloud environments with ephemeral, disposable instances) -- the standard solution is storing uploaded files in a dedicated object storage service like Amazon S3, which is durable, accessible from any application instance, and decoupled entirely from the application server's own lifecycle.
// Fragile: tied to this specific server's local disk
await fs.promises.writeFile(`./uploads/${filename}`, fileBuffer);
// Durable and scalable: stored in shared cloud object storage
await s3Client.send(new PutObjectCommand({ Bucket: 'uploads', Key: filename, Body: fileBuffer }));Real-world example A photo-sharing app that originally stored uploads on local disk lost user photos whenever a server instance was replaced during a routine deployment, until migrating to S3-based storage, after which uploaded files persisted reliably regardless of which server instance handled the original upload or any later requests for it.