forked from brakmic/OpenLok
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
41 lines (34 loc) · 1.07 KB
/
api.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
import Hapi from 'hapi';
import path from 'path';
import Inert from 'inert';
import BahnService from './src/app/services/BahnService';
require('es6-promise').polyfill();
require('isomorphic-fetch');
const server = new Hapi.Server();
const service = new BahnService({ useLocalApi: false });
server.connection({ port: 3000, routes: { cors: true } });
server.register(Inert, function () {});
server.route({
method: 'GET'
, path: '/locations/{location?}'
, handler: function(req, reply) {
let loc = req.params.location;
if (loc) {
return service.getLocations(loc)
.then(r => r.json())
.then(data => {
console.log(JSON.stringify(data, null, 4));
return reply(data);
})
.catch(err => {
console.log(JSON.stringify(err, null, 4));
return reply({ message: 'error', data: err });
});
}
reply('no result');
}
});
/* start server */
server.start(function() {
console.log('Server running at:', server.info.uri);
});