-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (30 loc) · 909 Bytes
/
index.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
const dotenv = require('dotenv')
dotenv.config()
const express = require('express')
const app = express()
const port = process.env.APP_PORT
const configuration = require('./knexfile.js')
const knex = require('knex')(configuration)
const database = process.env.SQL_DATABASE
const schema = process.env.SQL_SCHEMA
const table = process.env.SQL_TABLE
const target = `${database}.${schema}.${table}`
app.get('/citizen/:cpr', async (req, res) => {
const cpr = req.params.cpr
if (cpr.length !== 10 || !parseInt(cpr)) {
res.status(400).send('Bad Request')
} else {
knex(target)
.where({ CPR: cpr })
.first()
.then((data) => {
if (data === undefined || data.length === 0) {
res.status(404).send('Not found')
} else {
res.json(data)
}
}
)
}
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))