O meu banco de dados não está conectando. Ao fazer um refctoring do meu código seguindo o padrão MVC o banco de dados parou de funcionar.
Segue link com os arquivos para teste: https://github.com/LeonardoVini/node-refctoring
app.js
const app = require('./config/server')
// port must be set to 9095 because incoming http requests are routed from port 80 to port 9095
app.listen(9095, function () {
console.log('Node app is running on port 9095');
});
server.js
const express = require('express');
const consign = require('consign')
const bodyParser = require('body-parser');
const cors = require('cors');
const corsOptions = {
origin: 'http://localhost:4200',
optionsSuccessStatus: 200
}
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors(corsOptions))
consign()
.include('app/routes')
.then('config/dbConnection.js')
.then('app/models')
.then('app/controllers')
.into(app);
module.exports = app;
dbConnection.js
const mysql = require('mysql')
const connMySQL = () => {
return mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'eqix1998',
database: 'quotedb',
port: 3306
});
}
module.exports = () => {
return connMySQL;
}
Routes > quotes.js
module.exports = (application) => {
application.get('/quotes', (req, res) => {
application.app.controllers.quotes.quotes(application, req, res);
});
}
Controllers > quotes.js
module.exports.quotes = (application, req, res) => {
var connection = application.config.dbConnection();
var quotesDAO = new application.app.models.QuotesDAO(connection);
quotesDAO.getQuotes((error, results) => {
res.send(results)
});
}
Models > quotes.js
function QuotesDAO(connection){
this._connection = connection;
console.log(this._connection)
}
QuotesDAO.prototype.getQuotes = (callback) => {
this._connection.query('SELECT * FROM quotes', callback)
}
module.exports = () => {
return QuotesDAO;
}
Árvore de arquivos: