-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NodeJS service - move from mysql to postgres (#65)
* NodeJS service - move from mysql to postgres * Update package-lock.json
- Loading branch information
1 parent
76e55b4
commit 0497c38
Showing
5 changed files
with
164 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
const http = require('http'); | ||
const mysql = require('mysql2'); | ||
const http = require("http"); | ||
const { Client } = require("pg"); | ||
|
||
// create the connection to database | ||
const host = process.env.DB_HOST || 'localhost'; | ||
const connection = mysql.createConnection({ | ||
const host = process.env.DB_HOST || "localhost"; | ||
const client = new Client({ | ||
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 | ||
user: process.env.DB_USER || "postgres", | ||
password: process.env.DB_PASSWORD || "secret", | ||
database: process.env.DB_DATABASE || "score", | ||
port: process.env.DB_PORT || 5432, | ||
}); | ||
|
||
const requestHandler = async (request, response) => { | ||
|
@@ -17,40 +16,61 @@ const requestHandler = async (request, response) => { | |
const message = process.env.MESSAGE || "Hello, World!"; | ||
|
||
// Run hello world query | ||
const [rows, fields] = await connection.promise().query( | ||
'SELECT version() as version' | ||
const serverVersionRes = await client.query( | ||
`SHOW server_version;` | ||
); | ||
const version = rows[0].version; | ||
const serverVersion = serverVersionRes.rows[0].server_version; | ||
|
||
const versionRes = await client.query( | ||
`SELECT version();` | ||
); | ||
const version = versionRes.rows[0].version; | ||
|
||
const html = ` | ||
<html> | ||
<body> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity_no="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity_no="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity_no="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity_no="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> | ||
<h1>${message}</h1> | ||
<p>This is an application talking to a PostgreSQL <code>${serverVersion}</code> database on host <code>${host}</code>, deployed with Score!</p> | ||
<p><code></p> | ||
<p> | ||
<pre> | ||
SELECT version(); | ||
${version} | ||
</pre> | ||
</p> | ||
</div> | ||
</body> | ||
</html> | ||
` | ||
`; | ||
|
||
response.end(html); | ||
} | ||
}; | ||
|
||
const server = http.createServer(requestHandler); | ||
const App = async () => { | ||
// create the connection to database | ||
await client.connect(); | ||
|
||
const port = process.env.PORT || 8080; | ||
const server = http.createServer(requestHandler); | ||
|
||
server.listen(port, (err) => { | ||
if (err) { | ||
return console.log('something bad happened', err); | ||
} | ||
const port = process.env.PORT || 8080; | ||
|
||
console.log(`server is listening on ${port}`); | ||
}); | ||
server.listen(port, (err) => { | ||
if (err) { | ||
return console.log("something bad happened", err); | ||
} | ||
|
||
console.log(`server is listening on ${port}`); | ||
}); | ||
}; | ||
|
||
App(); | ||
|
||
// Exit the process when signal is received (For docker) | ||
process.on('SIGINT', () => { | ||
process.exit(); | ||
["SIGINT", "SIGTERM"].forEach((signal) => { | ||
process.on(signal, () => { | ||
process.exit(); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"name": "${{ values.name }}", | ||
"version": "1.0.0", | ||
"description": "A hello world app", | ||
"description": "An Hello World workload in NodeJS talking to a PostgreSQL database.", | ||
"main": "index.js", | ||
"license": "PRIVATE", | ||
"private": true, | ||
"dependencies": { | ||
"mysql2": "^3.9.7" | ||
"pg": "^8.13.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters