Anda mungkin perlu memeriksa nilai Anda di params. Tapi semoga ini dekat sampel minimal memberikan bantuan:
Perbarui
Dan itu membantu, karena menyoroti bahwa Anda sedang mencari _id
sebagai nama file. Sebaliknya Anda harus melakukan ini:
.createReadStream({
_id: req.param('filename')
})
jika tidak
.createReadStream({
_id: mongoose.Types.ObjectId(req.param('filename'))
})
Sejak _id
field berbeda dengan nama file
app.js
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var app = express();
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var conn = mongoose.createConnection('mongodb://localhost/mytest');
conn.once('open', function() {
console.log('opened connection');
gfs = Grid(conn.db);
// all environments
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(app.router);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
});
routes/index.js
exports.index = function(req, res){
res.set('Content-Type', 'image/jpeg');
gfs.createReadStream({
filename: 'receptor.jpg'
}).pipe(res);
};