-
Notifications
You must be signed in to change notification settings - Fork 634
/
bridge.js
33 lines (30 loc) · 1.09 KB
/
bridge.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
#!/usr/bin/env node
const cache = require('./cache')
const parse = require('url').parse
const router = {
qq: require('./provider/qq'),
xiami: require('./provider/xiami'),
baidu: require('./provider/baidu'),
kugou: require('./provider/kugou'),
kuwo: require('./provider/kuwo'),
migu: require('./provider/migu'),
joox: require('./provider/joox')
}
const distributor = (url, router) => Promise.resolve().then(() => {
const route = url.pathname.slice(1).split('/').map(path => decodeURIComponent(path))
let pointer = router, argument = decodeURIComponent(url.query)
try{argument = JSON.parse(argument)}catch(e){}
const miss = route.some(path => {
if(path in pointer) pointer = pointer[path]
else return true
})
if(miss || typeof pointer != 'function') return Promise.reject()
// return pointer.call(null, argument)
return cache(pointer, argument, 15 * 60 * 1000)
})
require('http').createServer().on('request', (req, res) =>
distributor(parse(req.url), router)
.then(data => res.write(data))
.catch(() => res.writeHead(404))
.then(() => res.end())
).listen(parseInt(process.argv[2]) || 9000)