-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
30 lines (26 loc) · 783 Bytes
/
Copy pathapp.js
File metadata and controls
30 lines (26 loc) · 783 Bytes
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
const express = require("express");
const mysql = require("mysql2/promise");
const app = express();
const port = 3000;
app.use(express.json());
//CONNECTION POOL
const pool = mysql.createPool({
host:'localhost',
user:'nodeuser',
password:'kezasabrine6^',
database:'quotesdB'
});
app.get('/',async(req,res)=>{
try{
const [rows] = await pool.query('SELECT * FROM quotes');
if(rows.length === 0) return res.send('No quotes found!');
const randomIndex = Math.floor(Math.random()*rows.length);
res.send(`Today's quote:<br>${rows[randomIndex].quote}`);
} catch (err) {
console.error(err);
res.status(500).send('Error in fetching quotes');
}
});
app.listen(port, () =>{
console.log('Server running');
});