Back to results for “Media”
Node.js › File Uploads & Media Processing

Why should CPU-intensive media processing like image resizing or video transcoding typically not run directly inside an Express request handler?

Intermediate

Answer

Media processing operations like resizing large images or transcoding video are genuinely CPU-intensive, and running them synchronously (or even via a Promise awaited directly) inside a request handler ties up that handler's processing time and, more importantly, can block the event loop if the underlying library isn't itself offloading work to a native thread pool -- the recommended pattern is to accept the upload quickly, enqueue the actual processing as a background job, and respond to the client immediately with a 202 Accepted and a way to check on the job's progress.
app.post('/upload', upload.single('video'), async (req, res) => {
  const jobId = await videoProcessingQueue.add('transcode', { filePath: req.file.path });
  res.status(202).json({ jobId, status: 'processing' });
});
Real-world example A video-upload endpoint immediately enqueues a transcoding job and responds with a 202 status and job ID, letting users check back later for the processed result, rather than making the HTTP request hang for however long a large video's transcoding actually takes.

Common follow-ups: How would sharp's or ffmpeg's own internal use of native threads change this calculus somewhat?;How does the client find out when the background processing job has completed?