3
My database isn’t connecting. When doing a refctoring of my code following the MVC standard the database stopped working.
Follow link with the test files: 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;
}
File tree:
it is quite possible that it is tied to the fact that the connection is an asynchronous function.
– Lucas Brogni
It would be nice if you put the code on Github or somewhere else so we can download and make things easier, for me there seems to be a problem in the way you are doing the connMySQL module Export, but I’ll just be sure by testing the code.
– viniciusxyz
Follow the link: https://github.com/LeonardoVini/node-refctoring
– Leonardo Vinicius
I use the angular for the front-end. I’m missing a connect() function. I have a code working, but all messy without following any Pattern designer and there I ran this function. However not being able to make it work in this case
– Leonardo Vinicius