Skip to content

Commit 2e17c44

Browse files
committed
Demo finished.
1 parent 848e8d8 commit 2e17c44

5 files changed

Lines changed: 1402 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
mysql-config.json

app.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Importing the dependencies
2+
const express = require('express');
3+
const cors = require('cors');
4+
const morgan = require('morgan');
5+
const mysql = require('mysql-await');
6+
const fs = require('fs');
7+
8+
// Init MySQL connection
9+
const connection = mysql.createConnection(JSON.parse(fs.readFileSync('mysql-config.json')));
10+
11+
connection.on('error', (err) => {
12+
console.error(`MySQL Connection error ${err.code}`);
13+
});
14+
15+
// Defining the Express app
16+
const app = express();
17+
const PORT = 3000;
18+
19+
// Calling the express.json() method for parsing
20+
app.use(express.json());
21+
22+
// Enabling CORS for all requests
23+
app.use(cors());
24+
25+
// Adding morgan to log HTTP requests
26+
app.use(morgan('combined'));
27+
28+
app.get('/:id', async (req, res) => {
29+
// connection.query()
30+
let result = await connection.awaitQuery(`SELECT * FROM node_test WHERE id = ?`, [req.params.id]);
31+
res.status(200).json({
32+
id: result[0].id,
33+
name: result[0].name
34+
});
35+
});
36+
37+
// starting the server
38+
app.listen(PORT, (err) => {
39+
if (err)
40+
console.log(err);
41+
console.log('Server listening on port', PORT);
42+
});

mysql-config-demo.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"host" : "example.org",
3+
"user" : "bob",
4+
"password" : "secret",
5+
"database" : "my_db"
6+
}

0 commit comments

Comments
 (0)