4
I recently made a refactor of my code to adopt the MVC standard, because it was very disorganized!
However, an error called "Too Many Connections" started to appear. This error causes my Node-mysql to crash and no more requests until I re-start Node.
Code link: https://github.com/LeonardoVini/node-refctoring/blob/master/refctoring-backend.zip
I don’t know if ES6 is causing this or if I did something wrong. I tried several times to fix this mistake.
Connection code:
const mysql = require('mysql')
const connMySQL = () => {
return mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'eqix1998',
database: 'quotedb',
port: 3306
});
}
module.exports = () => {
return connMySQL;
}
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;
Route archive:
module.exports = (application) => {
application.get('/quotes-createdBy/:username', (req, res) => {
application.app.controllers.quotes_by_name.getQuotesByName(application, req, res);
});
application.get('/quotes-closed-createdBy/:username', (req, res) => {
application.app.controllers.quotes_by_name.getClosedQuotesByName(application, req, res);
});
application.get('/quotes-qss-createdBy/:username', (req, res) => {
application.app.controllers.quotes_by_name.getQSSQuotesByName(application, req, res);
});
}
Controller file:
module.exports.getQuotesByName = (application, req, res) => {
let username = req.params.username
var connection = application.config.dbConnection();
var quotes_by_nameDAO = new application.app.models.Quotes_By_NameDAO(connection);
quotes_by_nameDAO.getQuotesByName(username, (error, results) => {
if (error) throw error
return res.send(results)
});
}
module.exports.getClosedQuotesByName = (application, req, res) => {
let username = req.params.username
var connection = application.config.dbConnection();
var quotes_by_nameDAO = new application.app.models.Quotes_By_NameDAO(connection);
quotes_by_nameDAO.getClosedQuotesByName(username, (error, results) => {
if (error) throw error
return res.send(results)
});
}
module.exports.getQSSQuotesByName = (application, req, res) => {
let username = req.params.username
var connection = application.config.dbConnection();
var quotes_by_nameDAO = new application.app.models.Quotes_By_NameDAO(connection);
quotes_by_nameDAO.getQSSQuotesByName(username, (error, results) => {
if (error) throw error
return res.send(results)
});
}
Model file:
function Quotes_By_NameDAO(connection) {
this._connection = connection;
}
// Get quotes by nome - QuoteStatus = 'In Progress' OR QuoteStatus = 'Stand By'
Quotes_By_NameDAO.prototype.getQuotesByName = function (username, callback) {
this._connection.query(`
SELECT * FROM quotes
WHERE
(QuoteStatus = 'In Progress' OR QuoteStatus = 'Stand By')
AND
CreatedBy = ?
`, username, callback)
}
// Get closed quotes by nome - QuoteStatus = 'Closed'
Quotes_By_NameDAO.prototype.getClosedQuotesByName = function (username, callback) {
this._connection.query(`
SELECT * FROM quotes
WHERE
QuoteStatus = 'Closed'
AND
CreatedBy = ?
`, username, callback)
}
// Get QSS quotes by nome - QuoteStatus = 'Sent to QSS'
Quotes_By_NameDAO.prototype.getQSSQuotesByName = function (username, callback) {
this._connection.query(`
SELECT * FROM quotes
WHERE
QuoteStatus = 'Sent to QSS'
AND
CreatedBy = ?
`, username, callback)
}
module.exports = () => {
return Quotes_By_NameDAO;
}
Can’t put the code where you make the connection here so we can evaluate?
– Sorack
I updated the question with the Iprs. I left the git link for visualization
– Leonardo Vinicius
I’ll rewrite a part of the structure of your application, okay? The route you access is like?
– Sorack
I added an example of how this is my Routes, controller and model.
– Leonardo Vinicius
Opa... can modify what is necessary. Thanks
– Leonardo Vinicius