-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (45 loc) · 1.74 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 mysql = require('mysql2');
// create the connection to database
const host = process.env.DB_HOST || 'localhost';
const connection = mysql.createConnection({
host,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'secret',
database: process.env.DB_DATABASE || 'score',
port: process.env.DB_PORT || 3306
});
const requestHandler = async (request, response) => {
console.log(request.url);
const message = process.env.MESSAGE || "Hello, World!";
// Run hello world query
const [rows, fields] = await connection.promise().query(
'SELECT version() as version'
);
const version = rows[0].version;
const html = `
<html>
<body>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<div class="container text-center mt-5 pt-5">
<h1>${message}</h1>
<p>This is an application talking to a MySQL <code>${version}</code> database on host <code>${host}</code>, deployed with Score!</p>
</div>
</body>
</html>
`
response.end(html);
}
const server = http.createServer(requestHandler);
const port = process.env.PORT || 8080;
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err);
}
console.log(`server is listening on ${port}`);
});
// Exit the process when signal is received (For docker)
process.on('SIGINT', () => {
process.exit();
});