Open willson-wang opened 6 years ago
app.js
import express from 'express'; import fs from 'fs'; import path from 'path'; import MovieRouter from './router/movieRouter' const bodyParser = require('body-parser'); const multer = require('multer'); const upload = multer(); // for parsing multipart/form-data const app = express(); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.use('/js', express.static('./views/js')); app.get('/', (req, res) => { res.sendFile(path.resolve('views/index.html'), { header: 'text/html' }) }); app.use('/movie', MovieRouter); app.listen(9527, () => { console.log('启用成功'); });
movieRouter.js
import express from 'express' import axios from 'axios' const router = express.Router(); const apiDomain = 'http://api.douban.com/v2/movie/'; router.get('/search', async(req, res) => { try { const { keyWord } = req.query; const result = await axios.get(`${apiDomain}search`, {params: {q: keyWord}}); res.json({ code: result.status, result: result.data }) } catch (err) { res.json({ code: -100086, error: error }) } }) /** * 榜单类接口,如Top250等 * */ router.get('/:type', async (req, res) => { try { const { type } = req.params; console.log('type', type) const result = await axios.get(`${apiDomain}${type}`); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 电影详情类接口 * */ router.get('/subject/:id', async(req, res) => { try { const { id } = req.params; console.log('id', id); const result = await axios.get(`${apiDomain}subject/${id}`); res.json({ code: result.status, result: result.data }) } catch (err) { res.json({ code: -100086, error: error }) } }) export default router;
musicRouter.js
import express from 'express' import axios from 'axios' const router = express.Router(); const apiDomain = 'http://tingapi.ting.baidu.com/v1/restserver/ting'; /** * 获取歌单 * * @type Number 类型 1-新歌榜,2-热歌榜,11-摇滚榜,12-爵士,16-流行,21-欧美金曲榜,22-经典老歌榜,23-情歌对唱榜,24-影视金曲榜,25-网络歌曲榜 * @size Number 返回条数 * @offset Number 当前页面 * */ router.get('/billList', async (req, res) => { try { const { type, size, offset } = req.query; console.log('type', type); const result = await axios.get(`${apiDomain}`, {params: {method: 'baidu.ting.billboard.billList', type, size, offset}}); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 图书搜索 * * @keyWord Sting | Number 关键字 * */ router.get('/search', async(req, res) => { try { const { keyWord } = req.query; console.log('keyWord', keyWord); const result = await axios.get(`${apiDomain}`, {params: {method: 'baidu.ting.search.catalogSug', query: keyWord}}); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 获取推荐列表 * * @song_id Number 歌曲id * @num Number 返回条数 * */ router.get('/recommandList', async (req, res) => { try { const { song_id, num } = req.query; console.log('song_id', song_id) const result = await axios.get(`${apiDomain}`, {params: {method: 'baidu.ting.song.getRecommandSongList', song_id, num}}); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 获取歌手信息 * * @tinguid Number 歌手id * */ router.get('/getMusicInfo', async(req, res) => { try { const { tinguid } = req.query; console.log('tinguid', tinguid); const result = await axios.get(`${apiDomain}`, {params: {method: 'baidu.ting.artist.getInfo', tinguid}}); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 获取歌手歌曲列表 * */ router.get('/getSongList', async(req, res) => { try { const { tinguid, limits, use_cluster, order } = req.query; console.log('tinguid', tinguid); const result = await axios.get(`${apiDomain}`, {params: {method: 'baidu.ting.artist.getSongList', tinguid, limits, order}}); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 获取歌词 * */ router.get('/getLry', async(req, res) => { try { const { songid } = req.query; console.log('songid', songid); const result = await axios.get(`${apiDomain}`, {params: {method: 'baidu.ting.song.playAAC', songid}}); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) export default router;
bookRouter.js
import express from 'express' import axios from 'axios' const router = express.Router(); const apiDomain = 'https://api.douban.com/v2/book/'; /** * 图书搜索 * */ router.get('/search', async(req, res) => { try { const { keyWord, tag, start, count } = req.query; console.log('keyWord', keyWord); const result = await axios.get(`${apiDomain}search`, {params: {q: keyWord, tag, start, count}}); console.log(result); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 根据id获取图书信息 * */ router.get('/:id', async (req, res) => { try { const { id } = req.params; console.log('idid', id) const result = await axios.get(`${apiDomain}${id}`); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) /** * 根据id返回丛书信息 * */ router.get('/isbn/:name', async(req, res) => { try { const { name } = req.params; console.log('name', name); const result = await axios.get(`${apiDomain}isbn/${name}`); res.json({ code: result.status, result: result.data }) } catch (error) { res.json({ code: -100086, error: error }) } }) export default router;
package.json
{ "name": "douban", "version": "1.0.0", "main": "index.js", "license": "MIT", "scripts": { "start": "babel-node app.js" }, "dependencies": { "axios": "^0.18.0", "body-parser": "^1.18.3", "express": "^4.16.3", "multer": "^1.3.1", "qs": "^6.5.2" }, "devDependencies": { "babel-cli": "^6.24.1", "babel-eslint": "^7.2.3", "babel-preset-es2015": "^6.24.1", "babel-preset-stage-2": "^6.24.1", "eslint": "^3.19.0", "eslint-config-standard": "^10.2.1", "eslint-friendly-formatter": "^3.0.0", "eslint-loader": "^1.7.1", "eslint-plugin-html": "^3.0.0", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.2.0", "eslint-plugin-promise": "^3.4.0", "eslint-plugin-standard": "^3.0.1" } }
app.js
movieRouter.js
musicRouter.js
bookRouter.js
package.json