I have DATA stored in mysql. And I want to display it in a table in HTML. I am currently using NODE.JS AND EXPRESS. How can I save the DATA that comes in and put it into a table in HTML. I was trying to find a way to save this DATA, And use a MAP loop inside a script tag in HTML, But I could not send the DATA to HTML .
app.js
const mysql = require('mysql')
const SQL = require('sql-template-strings')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000
const Joi = require('joi');
app.use(bodyParser.urlencoded({ extended: true }))
// connect to MySQL
const con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'pass123',
database: 'carStorege'
})
con.connect((err)=> {
if(err) throw err;
else console.log('connect to DB !')
})
// seva data from database
con.query("SELECT * from Cars", (err, result, fields) => {
if(err) console.log(err);
else {
//save the data
const data = result
}
})
// express
// url to see all the car in table
app.get('/all', (req, res) => {
con.query("SELECT * from Cars", (err, result, fields) => {
if(err) throw err;
else {
res.sendFile(__dirname+'/allCars.html')
}
})
})
app.listen(port, () => console.log('srever is live'))
and this is the HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<tr>
<th>Car manufacturer</th>
<th>Car model</th>
<th>Color</th>
<th>Yaer</th>
<th>Price</th>
<th>Door number</th>
</tr>
// here the data shuld be
</body>
</html>