-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (50 loc) · 1.31 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const http = require("http");
const { Pool } = require("pg");
const port = 1337;
const pool = new Pool({
host: "localhost",
database: "development"
});
const server = http
.createServer((req, res) => {
pool.connect((err, db, release) => {
try {
if (err) {
return res.status(500).send(err.toString());
}
let body = "";
req.on("data", chunk => {
body += chunk;
});
req.on("end", () => {
db.query(
`
select status, body, to_json(headers) as headers from app.main(
http.request(
method := $1,
path := $2,
body := $3
)
)`,
[req.method, req.url, body],
(err, result) => {
if (err) {
return res.writeHead(500).end(err.toString());
}
const {
rows: [{ status, body, headers }]
} = result;
const objHeaders = Object.fromEntries(
headers.map(({ name, value }) => [name, value])
);
return res.writeHead(status, objHeaders).end(body);
}
);
});
req.read();
} finally {
release();
}
});
})
.listen(port);