Express stops working after page reload 10 times

I use express as my web server for node, and everything seems to be working correctly. The only problem I encounter is when I load a specific page (route "/ learn") 10 times. As soon as I do this, the expression seems to stop working, although no error is logged on the console and nothing is displayed on the page. It just waits for the host in the browser. The strange thing is that the problem does not occur if I go from the page with the problem to another page, and then back. I can repeat it the way I want, without errors. Here is my route with the problem:

var bcrypt = require('bcrypt'); var pool = require('../database.js').pool; module.exports = function(app) { app.get('/learn', function(req, res, next) { var query = 'SELECT * FROM questions INNER JOIN answers ON questions.questionID = answers.questionID'; pool.getConnection(function(err, connection) { connection.query(query, function(err, rows) { if (err) { throw err; } var data = { name: req.session.name, problems: rows, }; res.render('learn.html', data); }); }); }); app.post('/learn/checkAnswer', function(req, res) { //get posted form data var questionID = req.body.questionID; var selectedAnswer = req.body.selectedAnswer; //query database pool.getConnection(function(err, connection) { var query = connection.query('SELECT correctAnswer FROM questions WHERE questionID = ?', questionID, function(err, rows) { res.send({ correctAnswer: rows[0].correctAnswer }); }); }); }); }; 

I'm not sure if that matters, but I use handlebars as my rendering engine instead of jade, as well as node-mysql for my database.

+4
source share
1 answer

10 - The default size of the node-mysql pool . And since you are not completing the connections received using pool.getConnection , the 11th request will wait an unlimited time for a free connection.

Easy to fix:

 connection.query(query, function(err, rows) { connection.end(); // end the connection as soon as possible, // so it returned to the pool and can be reused. if (err) ... }); 
+3
source

Source: https://habr.com/ru/post/1484014/


All Articles