Node.js 在生产环境中使用GridFS在节点中下载文件

Node.js 在生产环境中使用GridFS在节点中下载文件,node.js,mongodb,express,Node.js,Mongodb,Express,我有一个express应用程序,当我在本地运行它时,它就会工作。问题是在下载使用GridFS保存在mongoDB中的文件时。当在本地运行它时(我只需执行./bin/www并转到localhost:3000),我就可以下载该文件。但当我远程运行它时,我会下载一个html文件 这是处理响应的路由: router.get('/getfile',function(req,res) { if (req.isAuthenticated()) { var gfs = G

我有一个express应用程序,当我在本地运行它时,它就会工作。问题是在下载使用GridFS保存在mongoDB中的文件时。当在本地运行它时(我只需执行./bin/www并转到localhost:3000),我就可以下载该文件。但当我远程运行它时,我会下载一个html文件

这是处理响应的路由:

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');
});
这在jade文件中被这行调用:

td #[a(href="getfile?id=#{log.videoId}" download="video") #[span(name='video').glyphicon.glyphicon-download]]
在服务器上,我正在执行以下操作:

/logApp$ export NODE_ENV=production
/logApp$ ./bin/www 
mongoDB执事正在运行。事实上,我可以查询数据库。我没有写任何文件!我想读它

编辑:我发现了错误消息:
您需要将引导文件到响应的代码移动到
gfs.exist
回调中,以便它在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);
});

显然,如果文件不存在,就会出现一般性的“未打开写入”错误。

它可以工作,但现在在发出exists请求时挂起。我没有得到答案。gfs得到构建,_id在那里,但是gfs.exist调用挂起。它甚至不会产生错误。然而,我想这是另一个问题。你确实注意到了我的错误。谢谢。
handleError
函数是否可能没有任何帮助,只是吃了错误?还有,也许让它挂起30秒来排除超长超时时间的可能性。不,是另外一回事。我评论说,添加了所有的console.log打印。我想我保存这些文件的方式有问题。如果我找不到任何东西,我想我会看一下手册,然后再发一个问题。。。
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);
});