只用 axios 来实现:
var mp3_url = "http://other.player.nf01.sycdn.kuwo.cn/a8249eb428680c93e84d06971abba019/628c9fa9/resource/n1/57/86/2640963521.mp3"
const axios = require('axios').default;
const fs = require("fs")
axios({
method: "get",
url: mp3_url,
responseType: 'stream',
})
.then((res) => {
console.log("请求成功。。。。。")
var w_s = fs.createWriteStream("./static/xxx.mp3")
res.data.pipe(w_s)
}).catch((err) => {
console.log("请求出错")
console.log(err)
})
在 服务器路由中实现(本案例借助的是koa-router路由接口):
router.get('/:id/:mingcheng.mp3', async (ctx, next) => {
console.log("请求过来的参数")
console.log(ctx.params)
ctx.body = await new Promise((resolve, reject) => {
axios({
method: "get",
url: mp3_url,
responseType: 'stream',
})
.then((res) => {
resolve(res.data)
// var w_s = fs.createWriteStream("./static/xxx.mp3")
// res.data.pipe(w_s)
}).catch((err) => {
console.log("请求出错")
console.log(err)
reject(err)
})
})
});
网友评论