Skip to content

Commit

Permalink
简化路由导出方式
Browse files Browse the repository at this point in the history
  • Loading branch information
xqin committed May 31, 2018
1 parent 0f2988a commit 5b9b845
Show file tree
Hide file tree
Showing 94 changed files with 851 additions and 1,348 deletions.
6 changes: 5 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const UnusualRouteFileMap = {
"personal_fm.js": "/personal_fm"
};

// 简化 路由 导出方式, 由这里统一对 router 目录中导出的路由做包装, 路由实际对应的文件只专注做它该做的事情, 不用重复写样板代码
const { createWebAPIRequest, request } = require("./util/util");
const Wrap = fn => (req, res) => fn(req, res, createWebAPIRequest, request);

// 同步读取 router 目录中的js文件, 根据命名规则, 自动注册路由
fs.readdirSync("./router/").forEach(file => {
if (/\.js$/i.test(file) === false) {
Expand All @@ -61,7 +65,7 @@ fs.readdirSync("./router/").forEach(file => {
});
}

app.use(route, require("./router/" + file));
app.use(route, Wrap(require("./router/" + file)));
});

const port = process.env.PORT || 3000;
Expand Down
29 changes: 12 additions & 17 deletions router/album.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')

router.get('/', (req, res) => {
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
// 获取专辑内容
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
csrf_token: ''
}
const id = req.query.id
csrf_token: ""
};
const id = req.query.id;
createWebAPIRequest(
'music.163.com',
"music.163.com",
`/weapi/v1/album/${id}`,
'POST',
"POST",
data,
cookie,
music_req => {
res.send(music_req)
res.send(music_req);
},
err => res.status(502).send('fetch error')
)
})

module.exports = router
err => res.status(502).send("fetch error")
);
};
27 changes: 11 additions & 16 deletions router/artist_album.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')

router.get('/', (req, res) => {
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
const id = req.query.id
// 获取歌手专辑列表
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const id = req.query.id;
const data = {
offset: req.query.offset || 0,
total: true,
limit: req.query.limit || 30,
csrf_token: ''
}
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
"music.163.com",
`/weapi/artist/albums/${id}`,
'POST',
"POST",
data,
cookie,
music_req => res.send(music_req),
err => res.status(502).send('fetch error')
)
})

module.exports = router
err => res.status(502).send("fetch error")
);
};
26 changes: 10 additions & 16 deletions router/artist_desc.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')

router.get('/', (req, res) => {
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
const id = req.query.id
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const id = req.query.id;
const data = {
id,
csrf_token: ''
}
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
"music.163.com",
`/weapi/artist/introduction`,
'POST',
"POST",
data,
cookie,
music_req => res.send(music_req),
err => res.status(502).send('fetch error')
)
})

module.exports = router
err => res.status(502).send("fetch error")
);
};
11 changes: 3 additions & 8 deletions router/artist_list.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//分类歌单
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");

router.get("/", (req, res) => {
// 歌手分类
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";

// categoryCode 取值
Expand Down Expand Up @@ -42,6 +39,4 @@ router.get("/", (req, res) => {
},
err => res.status(502).send("fetch error")
);
});

module.exports = router;
};
26 changes: 10 additions & 16 deletions router/artist_mv.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')

router.get('/', (req, res) => {
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
const id = req.query.id
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const id = req.query.id;
const data = {
artistId: id,
total: true,
offset: req.query.offset,
limit: req.query.limit,
csrf_token: ''
}
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
"music.163.com",
`/weapi/artist/mvs`,
'POST',
"POST",
data,
cookie,
music_req => res.send(music_req),
err => res.status(502).send('fetch error')
)
})

module.exports = router
err => res.status(502).send("fetch error")
);
};
11 changes: 3 additions & 8 deletions router/artist_sub.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");

router.get("/", (req, res) => {
// 收藏歌手
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
artistId: `${req.query.id}`
Expand All @@ -18,6 +15,4 @@ router.get("/", (req, res) => {
},
err => res.status(502).send("fetch error")
);
});

module.exports = router;
};
11 changes: 3 additions & 8 deletions router/artist_sublist.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");

router.get("/", (req, res) => {
// 已关注歌手
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";

const data = {
Expand All @@ -21,6 +18,4 @@ router.get("/", (req, res) => {
},
err => res.status(502).send("fetch error")
);
});

module.exports = router;
};
11 changes: 3 additions & 8 deletions router/artist_unsub.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");

router.get("/", (req, res) => {
// 取消收藏歌手
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
artistId: `${req.query.id}`,
Expand All @@ -19,6 +16,4 @@ router.get("/", (req, res) => {
},
err => res.status(502).send("fetch error")
);
});

module.exports = router;
};
11 changes: 3 additions & 8 deletions router/artists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");

router.get("/", (req, res) => {
// 获取歌手单曲
module.exports = (req, res, createWebAPIRequest, request) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const id = req.query.id;

Expand All @@ -21,6 +18,4 @@ router.get("/", (req, res) => {
},
err => res.status(502).send("fetch error")
);
});

module.exports = router;
};
10 changes: 2 additions & 8 deletions router/banner.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const express = require("express");
const router = express();
const request = require("request");

router.get("/", (req, res) => {
module.exports = (req, res, createWebAPIRequest, request) => {
const options = {
url: "http://music.163.com/discover",
method: "GET",
Expand All @@ -25,6 +21,4 @@ router.get("/", (req, res) => {
}
}
});
});

module.exports = router;
};
34 changes: 14 additions & 20 deletions router/check_music.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
const express = require('express')
const router = express()
const request = require('request')
const { createWebAPIRequest } = require('../util/util')
router.get('/', (req, res) => {
const id = parseInt(req.query.id)
const br = parseInt(req.query.br || 999000)
module.exports = (req, res, createWebAPIRequest, request) => {
const id = parseInt(req.query.id);
const br = parseInt(req.query.br || 999000);
const data = {
ids: [id],
br: br,
csrf_token: ''
}
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
csrf_token: ""
};
const cookie = req.get("Cookie") ? req.get("Cookie") : "";

createWebAPIRequest(
'music.163.com',
'/weapi/song/enhance/player/url',
'POST',
"music.163.com",
"/weapi/song/enhance/player/url",
"POST",
data,
cookie,
music_req => {
if (JSON.parse(music_req).code == 200) {
return res.send({ success: true, message: 'ok' })
return res.send({ success: true, message: "ok" });
}
return res.send({ success: false, message: '亲爱的,暂无版权' })
return res.send({ success: false, message: "亲爱的,暂无版权" });
},
err => {
res.status(502).send('fetch error')
res.status(502).send("fetch error");
}
)
})

module.exports = router
);
};
28 changes: 11 additions & 17 deletions router/comment_album.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')

router.get('/', (req, res) => {
const rid = req.query.id
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
module.exports = (req, res, createWebAPIRequest, request) => {
const rid = req.query.id;
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
offset: req.query.offset || 0,
rid: rid,
limit: req.query.limit || 20,
csrf_token: ''
}
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
"music.163.com",
`/weapi/v1/resource/comments/R_AL_3_${rid}/?csrf_token=`,
'POST',
"POST",
data,
cookie,
music_req => {
res.send(music_req)
res.send(music_req);
},
err => res.status(502).send('fetch error')
)
})

module.exports = router
err => res.status(502).send("fetch error")
);
};
Loading

0 comments on commit 5b9b845

Please sign in to comment.