forked from williamoverton/score-simple-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (41 loc) · 1.4 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
const http = require('http');
const mysql = require('mysql2');
// create the connection to database
const connection = mysql.createConnection({
host: process.env.DB_HOST || 'localhost' ,
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);
// Run hello world query
const [rows, fields] = await connection.promise().query('SELECT "This is an example application deployed with Score!" as message');
const message = rows[0].message;
const html = `
<html>
<body>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<div class="container text-center mt-5 pt-5">
<h1>Hello World!</h1>
<p>${message}</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();
});