-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
32 lines (27 loc) · 845 Bytes
/
app.js
File metadata and controls
32 lines (27 loc) · 845 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
31
32
// app.js
const express = require('express');
const app = express();
// Set the view engine to EJS
app.set('view engine', 'ejs');
// Connect to the MySQL database
const mysql = require('mysql');
const pool = mysql.createPool({
host : 'localhost',
user : 'root',
password : 'kelvin273',
database : 'nodejs'
});
app.use(express.static(path.join(__dirname, '/public')));
// Define a route that will render the EJS template
app.get('/data', function(req, res) {
// Use a SELECT statement to retrieve data from the database
const sql = 'SELECT * FROM customer';
pool.query(sql, function(error, results, fields) {
if (error) throw error;
// Pass the results to the EJS template
res.render('index', { results: results });
});
});
app.listen(3000, function() {
console.log('App listening on port 3000!');
});