File upload to node using GridFS during production

I have an express application that works when I run it locally. The problem is loading a file that is saved in mongoDB using GridFS. When I run it locally (I just do. / Bin / www and switch to localhost: 3000), I can upload the file. But when I run it remotely, I load the html file.

This is the route that processes the response:

router.get('/getfile',function(req,res) {
    if (req.isAuthenticated())
    {
            var gfs = Grid(mongoose.connection, mongoose.mongo);
            var id = req.query.id;
            gfs.exist({_id: id}, function (err, found) {
                if (err) return handleError(err);
                if (!found)
                    res.send('Error on the database looking for the file.')
            });

            var readStream = gfs.createReadStream({
                _id: id
            }).pipe(res);
    }
    else
        res.redirect('/login');
});

which is called by this line in the jade file:

td #[a(href="getfile?id=#{log.videoId}" download="video") #[span(name='video').glyphicon.glyphicon-download]]

On the server, I do:

/logApp$ export NODE_ENV=production
/logApp$ ./bin/www 

works monoDB deamon. In fact, I can query the database. And I do not write any files! I want to read it.

EDIT: I found the error message:

MongoError: file with id #### not opened for writing
+4
source share
1

, gfs.exist, .

gfs.exist({ _id: id }, function(err, found) {
    if (err) {
      handleError(err); 
      return;
    }

    if (!found) {
      res.send('Error on the database looking for the file.')
      return;
    }

    // We only get here if the file actually exists, so pipe it to the response
    gfs.createReadStream({ _id: id }).pipe(res);
});

-, " " , .

+1

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


All Articles