我有一个index.js路由文件:
var express = require('express');
var router = express.Router();
var connection = require('../connection');
/* GET home page. */
router.get('/', function(req, res) {
res.render('layout', { title: 'Movieseat' });
connection.connect();
connection.query('SELECT * FROM `users` WHERE `first_name` = "Kees"', function(error, results){
console.log(
connection.end();
});
module.exports = router;
这需要我的connection.js文件:
// make a mysql connection to the database
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'us-cdbr-***-east-04.cleardb.net',
user : 'bfe4****0ede74',
password : '674****9',
database : 'heroku_****4ee0f0e9102'
});
module.exports = connection;
当我启动我的服务器时,它运行正常,但是当我重新加载我的localhost并执行router.get('/', function(req,res)
时,它会从数据库中返回正确的记录。但是30秒后服务器崩溃并出现以下错误:
events.js:
扔掉呃//未处理的错误'事件错误:退出不活动超时
答案 0 :(得分:0)
问题在于您的代码。
var express = require('express');
var router = express.Router();
var connection = require('../connection');
/* GET home page. */
router.get('/', function(req, res) {
res.render('layout', { title: 'Movieseat' });
connection.
connection.query('SELECT * FROM `users` WHERE `first_name` = "Kees"', function(error, results){
console.log(
connection.end();
});
module.exports = router;
您忘了关闭连接。我对node-postgres也有同感,每次查询数据库后关闭连接都很重要。
问候,Rafał