What I’ve done
- created
database.json
- put
/database.json
in.gitignore
to protect database information - updated
server.js
to fetch data from mySQL
const fs = require('fs'); // library that can aceess the file
const mysql = require('mysql'); // call mysql library
// ..
const data = fs.readFileSync('./database.json'); //read file
const conf = JSON.parse(data); //data parsing
const connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
port: conf.port,
database: conf.database
});
connection.connect();
app.get('/api/customers', (req, res) => {
connection.query(
"SELECT * FROM customer",
(err, rows, fields) => {
res.send(rows);
}
)
});
// ...
Error 1
- Not able to fetch data form mySQL database
- Empty table
- How did I fix? call data filled customer table
- the problem was the mySQL table name is a case sensitive
- had an empty CUSTOMER table and a data filled customer table
- called the empty CUSTOMER table first
Error 2
- Images error
- typo
https://placeimag.com/64/64/1
tohttps://placeimg.com/64/64/1