-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
38 lines (32 loc) · 980 Bytes
/
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
const express = require('express');
const app = express();
const pg = require('pg');
const connectionString = process.env.DATABASE_URL || 'postgresql://postgres:password@db:5432/postgres';
const client = new pg.Client(connectionString);
client.connect()
.then(() => console.log('connected'))
.catch(e => console.error('connection error', e.stack));
app.get('/number', (req, res) => {
client.query("select number from magicNumbers;", (err, result) => {
res.send(result.rows.map((row) => row.number));
});
})
app.get('/health', (req, res) => {
res.status(200);
res.end();
})
app.post('/number', (req, res) => {
let number;
req.on('data', (data) => {
number = JSON.parse(data.toString()).number;
})
req.on('end', () => {
client.query(
`INSERT INTO magicNumbers values(${number});`, (err, res) => {
err && console.log(err);
res.send("acknowledged");
});
})
});
app.listen(8000);
app.on('error', () => client.end());