-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (53 loc) · 1.73 KB
/
app.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
import DataServiceClient from './DataServiceClient';
import { promises as fsp } from 'fs';
import Koa from 'koa';
import Router from '@koa/router';
import logger from 'koa-logger';
const APP_PORT = 4040;
const app = new Koa();
var router = new Router();
app.use(logger());
router.get('/', (ctx, next) => {
ctx.body = 'REST/JSON version of HR Dataservice';
});
router.get('/api/:kvkNummer', async (ctx, next) => {
let args = {
kvkNummer: ctx.params.kvkNummer, // '90004426', // '90000021'
// klantreferentie: 'Mayer Software Developement',
};
try {
ctx.body = await fsp.readFile(`./cache/${args.kvkNummer}.json`, 'utf8');
return;
} catch (error) {
console.log('not in cache: ', args.kvkNummer);
}
try {
let result = await DataServiceClient('ophalenInschrijving', args);
// await fsp.writeFile('./tmp2.xml', result[3], 'utf8');
// await fsp.writeFile('./tmp2.json', JSON.stringify(result[2]), 'utf8');
if (result[0].meldingen.fout) {
ctx.status = 404;
ctx.body = { message: result[0].meldingen.fout[0].omschrijving };
return;
}
if (result[0].meldingen.informatie[0].code !== 'IPD0000') {
ctx.status = 404;
ctx.body = { message: result[0].meldingen.informatie[0].omschrijving };
return;
}
ctx.body = result[0].product.maatschappelijkeActiviteit;
await fsp.writeFile(
`./cache/${args.kvkNummer}.json`,
JSON.stringify(result[0].product.maatschappelijkeActiviteit),
'utf8'
);
} catch (error) {
console.log(error.message);
ctx.status = 500;
ctx.body = error.message;
}
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
});