-
Notifications
You must be signed in to change notification settings - Fork 199
/
dev-server.js
175 lines (150 loc) · 5.13 KB
/
dev-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require('./check-versions')()
var config = require('../config')
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
}
var opn = require('opn')
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = require('./webpack.dev.conf')
var http = require('http');
// default port where dev server listens for incoming traffic
var port = process.env.PORT || config.dev.port
// automatically open browser, if not set will be false
var autoOpenBrowser = !!config.dev.autoOpenBrowser
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var proxyTable = config.dev.proxyTable
var app = express()
var data = require('../music-data.json');
// var musicData = data.musicData;
var apiRoutes = express.Router();
apiRoutes.get('/music-data', function (req, res) {
res.json({
errno: 0,
musicData: data.musicData
});
});
// 获取 One 的接口
apiRoutes.get('/one/:page?', function (req, res) {
let page = +req.params.page || 0;
let url = 'http://v3.wufazhuce.com:8000/api/onelist/idlist/?channel=wdj&version=4.0.2&uuid=ffffffff-a90e-706a-63f7-ccf973aae5ee&platform=android';
let idList = '';
let getIdList = new Promise((resolve, reject) => {
http.get(url, response => {
response.on('data', data => {
idList += data;
});
response.on('end', () => {
resolve(idList);
});
})
});
let getOne = function(id) {
let result = '';
let url = 'http://v3.wufazhuce.com:8000/api/onelist/' + id +'/0?channel=wdj&version=4.0.2&uuid=ffffffff-a90e-706a-63f7-ccf973aae5ee&platform=android';
return new Promise((resolve, reject) => {
http.get(url, response => {
response.on('data', data => {
result += data;
});
response.on('end', () => {
resolve(result);
})
})
});
}
getIdList.then(idList => JSON.parse(idList).data)
.then(data => {
return Promise.all( data.map((item) => {
return getOne(item).then(data => JSON.parse(data));
}))
})
.then(list => {
list = JSON.parse(JSON.stringify(list));
res.json(list[page]);
})
})
apiRoutes.get('/search/:num/:name', (req, res) => {
let num = req.params.num;
let name = req.params.name;
function search(n, keywords) {
return new Promise((resolve, reject) => {
let searchResult = '';
let url = encodeURI('http://s.music.qq.com/fcgi-bin/music_search_new_platform?t=0&n='+ n +'&aggr=1&cr=1&loginUin=0&format=json&inCharset=GB2312&outCharset=utf-8¬ice=0&platform=jqminiframe.json&needNewCode=0&p=1&catZhida=0&remoteplace=sizer.newclient.next_song&w='+ keywords);
http.get(url, response => {
response.on('data', data => {
searchResult += data;
});
response.on('end', () => {
resolve(searchResult);
})
})
})
}
search(num, name)
.then(searchResult => {
res.json(JSON.parse(searchResult));
})
});
apiRoutes.get('/hot', (req, res) => {
let hotKeywords = ['歌手', '张杰', '赵雷', '李健', '林志炫', '张碧晨', '梁博', '周笔畅', '张靓颖', '陈奕迅', '周杰伦', '王力宏', 'TFBoys', '李玉刚', '魏晨', '薛之谦'];
let rHot = new Array(6);
for (let i = 0; i < rHot.length; i++) {
let length = hotKeywords.length;
let random = Math.floor(length * Math.random());
rHot[i] = hotKeywords[random];
hotKeywords.splice(random, 1);
}
res.json(rHot);
});
app.use('/api', apiRoutes);
var compiler = webpack(webpackConfig)
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: true
})
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
log: () => {}
})
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
hotMiddleware.publish({ action: 'reload' })
cb()
})
})
// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
var options = proxyTable[context]
if (typeof options === 'string') {
options = { target: options }
}
app.use(proxyMiddleware(options.filter || context, options))
})
// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// serve webpack bundle output
app.use(devMiddleware)
// enable hot-reload and state-preserving
// compilation error display
app.use(hotMiddleware)
// serve pure static assets
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static'))
var uri = 'http://localhost:' + port
devMiddleware.waitUntilValid(function () {
console.log('> Listening at ' + uri + '\n')
})
module.exports = app.listen(port, function (err) {
if (err) {
console.log(err)
return
}
// when env is testing, don't need open it
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
})