-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-server.js
61 lines (54 loc) · 1.55 KB
/
proxy-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
const Koa = require('koa');
const Router = require('koa-router');
const url = require('url');
const request = require('request');
const cors = require('koa2-cors');
const koaBody = require('koa-body');
const serve = require('koa-static');
const app = new Koa();
const router = new Router();
const getParams = urlStr => url.parse(urlStr, true).query;
const getUrl = ({lang, suffix}) => {
return `http://booking.uz.gov.ua/${lang}/purchase/${suffix}`;
}
router
.get('/:lang/cities', function (ctx, next) {
const {lang} = ctx.params;
const qs = getParams(ctx.req.url);
const url = getUrl({lang, suffix: 'station/'});
return new Promise(resolve => {
request.get({url, qs, json:true}, (err, res, body) => {
if (err) {
return next(err);
}
ctx.body = body;
resolve();
});
})
})
.post('/:lang/tickets', function (ctx, next) {
const {lang} = ctx.params;
const form = ctx.request.body;
///https://github.com/request/request/issues/2619
form.hasOwnProperty = k => Object.prototype.hasOwnProperty.call(formData, k);
const url = getUrl({lang, suffix: 'search/'});
return new Promise(resolve => {
request.post({url, form}, (err, res, body) => {
if (err) {
return next(err);
}
ctx.body = body;
resolve();
});
})
})
;
app
.use(serve('dist'))
.use(koaBody())
.use(cors())
.use(router.routes())
.use(router.allowedMethods())
;
app.listen(process.env.PORT || 5000);
console.log('Start server at port ', process.env.PORT || 5000);