router.get('/play/:song_id', async ctx => {
try {
const sql = `SELECT location FROM songs WHERE song_id = ${ctx.params.song_id} LIMIT 1;`
const db = await sqlite.open(dbName)
const data = await db.get(sql)
await db.close()
const newdata = JSON.parse(JSON.stringify(data))
console.log(newdata)
ctx.response.type = 'mp3'
ctx.response.body = fs.createReadStream(newdata)
} catch (err) {
ctx.body = err.message
}
})
这是我得到的输出:
{ location: 'public/songs/Santeria.mp3' }
但是我只想要'public/songs/Santeria.mp3'
部分(该列的值)。
您只需要更改行:
ctx.response.body = fs.createReadStream(newdata)
to
ctx.response.body = newdata.location