我的代碼:如何從使用gridFS存儲的mongodb中讀取mp3文件?
storeAudio.js
var mongoose = require('mongoose');
var fs = require('fs');
var Grid = require('gridfs-stream');
Grid.mongo=mongoose.mongo;
//establish mongoDB connection
mongoose.Promise = global.Promise;
var conn = mongoose.createConnection('mongodb://localhost:27017/aHolyBoly');
conn.once('open',function(){
var gfs = Grid(conn.db);
// var db = new mongo.Db('aHolyBoly', new mongo.Server("127.0.0.1", 27017));
//var gfs = Grid(db, mongo);
var writeStream = gfs.createWriteStream({
filename:'song1.mp3'
});
fs.createReadStream('../list/hero.mp3').pipe(writeStream);
writeStream.on('close',function(file){
console.log(file.filename +'Written to db');
});
});
我的MP3文件在數據庫成功寫入。 現在我的目標是創建使用快遞的路線,以便我可以在我的angular2應用程序中使用它作爲提供商的API。
server.js
var mongoose = require('mongoose');
var fs = require('fs');
var Grid = require('gridfs-stream');
Grid.mongo=mongoose.mongo;
//establish mongoDB connection
mongoose.Promise = global.Promise;
var conn = mongoose.createConnection('mongodb://localhost:27017/aHolyBoly');
conn.once('open',function(){
var gfs = Grid(conn.db);
// var db = new mongo.Db('aHolyBoly', new mongo.Server("127.0.0.1", 27017));
//var gfs = Grid(db, mongo);
var writeStream = gfs.createWriteStream({
filename:'song1.mp3'
});
fs.createReadStream('../list/hero.mp3').pipe(writeStream);
writeStream.on('close',function(file){
console.log(file.filename +'Written to db');
});
});
這裏的某個地方在我的代碼,我無法找到寫在DB使用GridFS的歌曲。 gridFS的鏈接API gridFS API Link
我想創建一個使用ionic2的應用程序,它包含一個搜索欄,當我輸入歌曲名稱時它會顯示歌曲,我可以播放它。
我掙扎了1周,在堆棧上寫了3次,但無法得到答案。
您可以將我重定向到GitHub倉庫或某個地方下載您的整個代碼並嘗試調試嗎? –