<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<audio src="" class="my_audio" ></audio>
<!-- controls -->
<input class="seach" type="text" value="linjunjie" />
<button class="dian">搜索</button>
<div>
<ul class="music_wrap">
</ul>
</div>
</body>
</html>
<script>
var my_audio = document.querySelector(".my_audio");
var btn = document.querySelector(".dian")
var music_wrap = document.querySelector(".music_wrap")
var svalue = document.querySelector(".seach")
btn.onclick = function(){
seachs()
}
document.onkeydown = function(){
if(svalue.value.length >0 && event.key == "Enter")
seachs()
}
function seachs(){
var req = new XMLHttpRequest()
req.open("GET","http://localhost:3000/abc?ss="+svalue.value,true)
req.send()
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status == 200){
var music_list = JSON.parse(req.responseText);
music_list = music_list.data.list
console.log(music_list)
var big_string = ""
for(var i =0 ; i<music_list.length;i++){
var a = music_list[i].name
var b = music_list[i].artist
var c = music_list[i].rid
big_string = big_string + '<li>名称:' + a+'歌手'+ b+'<button οnclick="f_mp3('+c+')">播放</button></li>'
}
music_wrap.innerHTML = big_string
}
}
}
function f_mp3(rid){
var req = new XMLHttpRequest()
req.open("GET","http://localhost:3000/bofang?rid="+rid,true)
req.send()
req.onreadystatechange=function(){
if(req.readyState==4 && req.status==200){
var music_data = JSON.parse(req.responseText)
var music_url = music_data.url
my_audio.src = music_url
my_audio.play()
}
}
}
</script>
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
const serve = require('koa-static');
const koaBody = require('koa-body');
const fs = require('fs');
const request = require('request');
app.use(koaBody({
multipart: true,
formidable: {
maxFieldsSize: 1024 * 1024 * 10
}
}));
const router = new Router();
app.use(serve(__dirname + '/static'));
router.get('/', (ctx, next) => {
ctx.body = "服务器主界面";
});
router.get('/bofang', async(ctx, next) => {
var rid = ctx.request.query.rid
var format_mp3_url = "http://www.kuwo.cn/url?format=mp3&rid="+rid+"&response=url&type=convert_url3&br=128kmp3&from=web&t=1625887047798&httpsStatus=1&reqId=5acb4161-e12d-11eb-87e1-ad48250e289b"
var a = new Promise(function(aaa, bbb) {
request.get({
url: format_mp3_url,
},
function(err, res, body) {
if (err) {
bbb(err)
} else {
aaa(body)
}
})
})
ctx.body = await a
});
router.post('/upload', (ctx, next) => {
var xxx = ctx.request.files
var file_path = xxx.sx.path
var r = fs.createReadStream(file_path)
var w = fs.createWriteStream("./static/666.jpg")
r.pipe(w)
ctx.body = "9999999"
});
router.get('/abc', async(ctx, next) => {
var key_word = ctx.request.query.ss
key_word = encodeURIComponent(key_word)
var kuwo = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key="+key_word+"&pn=1&rn=30&httpsStatus=1&reqId=c7d7dbd1-e09a-11eb-a771-8544d2baeb57"
var tou = {
"Cookie": "_ga=GA1.2.961517434.1625790913; _gid=GA1.2.2055520088.1625790913; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1625790913,1625792490,1625824054; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1625824054; _gat=1; kw_token=XYIZCHNKQST",
"csrf": "XYIZCHNKQST",
"Host": "www.kuwo.cn",
"Referer": "http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
var a = new Promise(function(aaa, bbb) {
request.get({
url: kuwo,
headers: tou
},
function(err, res, body) {
if (err) {
bbb(err)
} else {
aaa(body)
}
})
})
ctx.body = await a
});
router.get('/test', (ctx, next) => {
ctx.body = "服务器test界面";
});
console.log("server is running on 3000");
console.log("you can ctrl + c end(shutdown) ");
console.log("you can brower http://localhost:3000");
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);