-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (39 loc) · 1.05 KB
/
app.js
File metadata and controls
47 lines (39 loc) · 1.05 KB
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
const http = require("http");
const process = require("process");
const { Client } = require("pg");
const client = new Client();
client.connect();
const hostname = "0.0.0.0";
const port = 3000;
process.on("SIGINT", (code) => {
console.log("Process exit event with code: ", code);
client.end();
process.exit();
});
// https://node-postgres.com/features/connecting
var getMessageFromDB = function (cb) {
client.query("SELECT * FROM messages", (err, res) => {
if (err) {
console.log(err.stack);
cb([]);
} else {
cb(res.rows);
}
});
};
const server = http.createServer((req, res) => {
console.log("request received");
getMessageFromDB(function (messages) {
var html_text = "<ul>";
for (const message of messages) {
html_text += "<li>" + message["text"] + "</li>";
}
html_text += "</ul>";
res.statusCode = 200;
res.setHeader("Content-Type", "text/html");
res.end(html_text);
});
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});