Show BASE64 video with node/express
<p>So, bit of an odd problem. I have a bunch of media files saved as base64 strings in mongo, some are images, some are videos.</p>

<p>I made an API for getting the media files:</p>

<pre><code>app.get('/api/media/:media_id', function (req, res) {
    media.findById(req.params.media_id)
        .exec(function (err, media) {
            if (err) {
                res.send(err);
            }

            var file = new Buffer(media.file, 'base64');
            res.writeHead(200, {'Content-Type': media.type, 'Content-Transfer-Encoding': 'BASE64', 'Content-Length': file.length});
            res.end(file);
        });
});
</code></pre>

<p>Now, images have no problems. They load just fine, both directly from the API, and when I call the API from a front-end (for example <code>&lt;img src="/api/media/23498423"&gt;</code>)</p>

<p>THE PROBLEM</p>

<p>If I fetch a video from a front-end, like the images - but with a video- or object-tag:</p>

<pre><code>&lt;video src="/api/media/3424525" controls&gt;&lt;/video&gt;
</code></pre>

<p>there's no problem, but if I load the video in a browser directly from the API:</p>

<pre><code>http://localhost:8080/api/media/3424525
</code></pre>

<p>the server process crashes, no errors. It simply just freezes up. And we're not talking about huge video files - it's a 1.5MB video.</p>

<p>The media type in the header for all the videos I'm testing with is <code>video/mp4</code>. Oh, and just to be clear: if I do the same with images, everything works perfectly.</p>

<p><strong>EDIT:</strong></p>

<p>Okay, so as suggested by @idbehold and @zeeshan I took a look at gridfs and gridfs-stream, and for the purpose of my app, this certainly is what I should have used in the first place. However, after implementing gridfs in my app, the problem still persists.</p>

<pre><code>app.get('/api/media/:media_id', function (req, res) {
    gfs.findOne({ _id: req.params.media_id }, function (err, file) {
        if (err) {
            return res.status(400).send(err);
        }
        if (!file) {
            return res.status(404).send('');
        }

        res.set('Content-Type', file.contentType);
        res.set('Content-Disposition', 'inline; filename="' + file.filename + '"');

        var readstream = gfs.createReadStream({
            _id: file._id
        });

        readstream.on("error", function (err) {
            console.log("Got an error while processing stream: ", err.message);
            res.end();
        });

        readstream.pipe(res);
    });
});
</code></pre>

<p>When I call the media file (be it image or video) from a front-end, within a HTML tag, everything works out fine. But if I load a video (again, smallish videos from 1.5mb to max 6mb total size) directly in the browser, the server process freezes. To be a bit more clear: I am testing on windows, and the server app (server.js) is run in console. The console and the process it is running is what freezes. I cannot load any more pages/views in the node app, and I cannot even stop/kill/shutdown the node app or the console.</p>