-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (46 loc) · 1.25 KB
/
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
const koa = require('koa')
const router = require('koa-router')()
const logger = require('koa-logger')
const webpackDevServer = require('koa-webpack-dev')
const yahooFinance = require('yahoo-finance')
const xray = require('x-ray')
const app = koa()
app.use(logger())
router.get('/api/quote', function * () {
const beginDate = this.query.beginDate
const endDate = this.query.endDate
const period = this.query.period
const inputSymbols = (this.query.symbols).split(',')
this.body = yield yahooFinance.historical({
symbols: inputSymbols,
from: beginDate,
to: endDate,
period
}).then(response =>
response
).catch(error => {
this.body = { message: error.message }
this.status = error.status || 500
})
})
router.get('/api/split', function * () {
const x = xray()
const url = 'https://au.finance.yahoo.com/q/bc?s='
const symbol = this.query.symbol
this.body = yield new Promise(resolve =>
x(`${url}${symbol}`, 'nobr', ['nobr'])((error, splitHistory) =>
resolve(splitHistory)
)
).then(response =>
response
).catch(error => {
this.body = { message: error.message }
this.status = error.status || 500
})
})
app.use(router.routes())
app.use(router.allowedMethods())
app.use(webpackDevServer({
config: './webpack.config.js'
}))
app.listen(2333)