我使用express作为节点的web服务器,一切似乎都正常工作.我遇到的唯一问题是我多次加载特定页面(‘/ learn’路线)10次.一旦我这样做,express似乎停止工作,虽然没有错误记录到控制台,并且页
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 }); }); }); }); };
我不确定这是否有所作为,但我使用把手作为我的渲染引擎而不是jade,以及我的数据库的node-mysql.
10 is the default size of thenode-mysql
pool.由于您没有结束使用pool.getConnection检索的连接,因此第11个请求将无限期地等待免费连接.
易于修复:
connection.query(query, function(err, rows) { connection.end(); // end the connection as soon as possible, // so it's returned to the pool and can be reused. if (err) ... });