Reading file from gridfs in mongodb

I have this route for this task:

var mongoose = require('mongoose');
var gfs;
var Grid = require('gridfs-stream');
var connection = mongoose.createConnection(mongodb://localhost/images);
connection.once('open', function () {
    gfs = Grid(connection.db, mongoose.mongo);
});
app.get('/api/image/:file', function(req, res) {
    res.set('Content-Type', getType(req.params.file));
    gfs.createReadStream({
        filename: '/api/image/'+req.params.file
    }).pipe(res);
});

For some reason, it is really slow, cannot understand why this is so. Even when I run it on local. If I use something like mongoose-fs or something familiar, will it be faster? Also, every time you download an image, it does not save this image in the cache (200 each time). How can I tell node to cache this? At the moment I have an expressive way app.set('view cache', true);.

+4
source share

Source: https://habr.com/ru/post/1599656/


All Articles