Hello! I would like to know a way to present to the user in an HTML table, data stored in a MYSQL database with connection through JSON Express.
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = 3309;
const mysql = require('mysql');
var formidable = require("formidable");
var obj = {};
var path = require('path');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const router = express.Router();
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/lista', function(req, res) {
execSQLQuery("SELECT Nome, modeloCarro, status FROM Clientes",res);
});
app.post('/dados', function(req, res) {
execSQLQuery("INSERT INTO Clientes(Nome,CPF,dtNascimento,modeloCarro,status) VALUES ('"+req.body.nomeMotorista+"','"+req.body.CPF+"','"
+req.body.dataNascimento+"','"+req.body.modeloCarro+"','"+req.body.Ativo+"')", res);
});
app.listen(port);
console.log('API funcionando!');
/ ***************************** Connection Code ************** *********************** /
function execSQLQuery(sqlQry, res){
const connection = mysql.createConnection({
host : 'localhost',
port : 3308,
user : 'root',
password : 'teste',
database : 'mysql'
});
connection.query(sqlQry, function(error, results, fields){
if(error)
res.json(error);
else
res.json(results);
connection.end();
console.log('executou!');
});
}