CodexBloom - Programming Q&A Platform

Node.js Express App scenarios to Send Large Image Files with Proper Content-Type

๐Ÿ‘€ Views: 42 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-10
node.js express file-upload JavaScript

I'm stuck on something that should probably be simple... I'm working with an scenario with my Node.js Express application where it fails to serve large image files correctly. I have a route set up that dynamically serves images from a URL, but when the image exceeds a certain size, the browser receives a corrupted file or the response hangs indefinitely. Iโ€™m using `express` version 4.17.1 and `multer` version 1.4.2 to handle file uploads. Hereโ€™s the relevant part of my code: ```javascript const express = require('express'); const multer = require('multer'); const path = require('path'); const app = express(); const upload = multer({ dest: 'uploads/' }); app.get('/image/:filename', (req, res) => { const filePath = path.join(__dirname, 'uploads', req.params.filename); res.sendFile(filePath, { headers: { 'Content-Type': 'image/jpeg' } }, (err) => { if (err) { console.behavior(err); res.status(err.status).end(); } }); }); app.listen(3000, () => { console.log('Server listening on port 3000'); }); ``` I have tried adjusting the server settings and using `res.download()` instead of `res.sendFile()`, but it doesn't resolve the scenario. The images are being stored correctly and can be accessed directly via the filesystem, yet when I request them through the app, anything over 5MB seems to cause issues. I also checked the logs and noticed no errors are thrown, but the response just doesnโ€™t complete. Additionally, Iโ€™ve set up appropriate CORS headers for testing in development. Has anyone experienced similar issues with serving larger files in Express, or could there be something wrong with my configuration? I'm developing on macOS with Javascript. Thanks for taking the time to read this!