我的MySQL连接遇到了一些困难:
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const db = require('./config/db');
// Init App
const app = express();
//msql connection
db.connect(function(err){
if(err){
throw err;
}
console.log('MySQL Connected...');
});
// View Engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Bodyparser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// Set Static path
app.use(express.static(path.join(__dirname, 'static')));
// Session middleware
app.use(session({
secret: 'stuffedbagels',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}));
// Express Messages middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// Global Variables
app.use(function(req, res, next){
res.locals.errors = null;
next();
});
// Express Validator Middleware
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
// Routes
// Home
app.get('/', function(req, res) {
res.render('index');
});
// Register
app.post('/config/register', function(req, res){
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('kingname', 'Kingdom Name is Required').notEmpty();
req.checkBody('email', 'E-Mail is Required').notEmpty();
req.checkBody('password', 'Password is Required').notEmpty();
req.checkBody('password2', 'Password Confirmation is Required').notEmpty();
var errors = req.validationErrors();
if(errors) {
res.render('index', {
errors: errors
});
} else {
var newUser = {
username: req.body.username,
kingname: req.body.kingname,
email: req.body.email,
password: req.body.password,
password2: req.body.password2
}
// let post = {username: newUser.username, password: newUser.password, email: newUser.email, kingname: newUser.kingname};
// let sql ='INSERT INTO users SET ?';
// let query = db.query(sql, post, function(err, res){
// if(err) throw err;
// user_id = res.insertID;
// console.log('Last User ID', res.insertID);
// });
console.log('User registration successful...');
res.redirect('/');
}
});
app.listen(8080, function(){
console.log('Server listening on port 8080...')
})
我在模块中有MySQL连接:
const mysql = require('mysql');
module.exports = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'pass',
database: 'table'
});
现在我收到了这个错误:
events.js:183 扔掉//未处理的'错误'事件 ^
Error: Connection lost: The server closed the connection. at Protocol.end (/home/kogadmin/www/node_modules/mysql/lib/protocol/Protocol.js:113:13) at Socket.<anonymous> (/home/kogadmin/www/node_modules/mysql/lib/Connection.js:109:28) at emitNone (events.js:111:20) at Socket.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1056:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9) [nodemon] app crashed - waiting for file changes before starting...
正如你所看到的那样,我注释掉了我的表插入,因为我认为这是导致它的,但我可以理解,为了我的生命,为什么要这样做。
值得注意的是,我使用的是nginx作为反向代理。
答案 0 :(得分:0)
我发现了一个非常可疑的答案:
// Keep MySQL Alive
setInterval(function () {
db.query('SELECT 1');
}, 5000);
虽然这似乎有效,但我不必每隔x个时间查询一次我的服务器。我宁愿它一直都是连接起来的。