app.get('/api/:id', (req, res) => { var bucket = new mongodb.GridFSBucket(mydb, { bucketName: 'ls-config', }) var downStream = bucket.openDownloadStream(req.params.id); downStream.pipe(res); })
function getTrack(req, res) { try { const trackID = new ObjectID(req.params.trackID); res.set('content-type', 'audio/mp3'); res.set('accept-ranges', 'bytes'); getDatabase().then((db) => { const bucket = new mongodb.GridFSBucket(db, { bucketName: 'tracks', }); const downloadStream = bucket.openDownloadStream(trackID); downloadStream.on('data', (chunk) => { res.write(chunk); }); downloadStream.on('error', () => { res.sendStatus(404); }); downloadStream.on('end', () => { res.end(); }); }); } catch (err) { return res.status(400).json({ message: 'Invalid trackID in URL parameter. Must be a single String of 12 bytes or a string of 24 hex characters' }); } }
/** * GET /tracks/:trackID */ trackRoute.get('/:trackID', (req, res) => { try { var trackID = new ObjectID(req.params.trackID); } catch(err) { return res.status(400).json({ message: "Invalid trackID in URL parameter. Must be a single String of 12 bytes or a string of 24 hex characters" }); } res.set('content-type', 'audio/mp3'); res.set('accept-ranges', 'bytes'); let bucket = new mongodb.GridFSBucket(db, { bucketName: 'tracks' }); let downloadStream = bucket.openDownloadStream(trackID); downloadStream.on('data', (chunk) => { res.write(chunk); }); downloadStream.on('error', () => { res.sendStatus(404); }); downloadStream.on('end', () => { res.end(); }); });