I make a call to Mysql.createPool and it simply returns nothing. Every instance I have attempted using it either lies dormant or gets skipped in favour of processes that do not require it during their execution. The server itself returns no errors so I cannot tell what the cause is. It just crashes after some time with the message:
throw err;
^Error: Connection lost: The server closed the connection.
on this line
myPool.getConnection(function(err, connection) {
if (err) {
throw err;
}
I have already read the docs, mysql issues and a gazillion online tutorials on this Node.js module but none seems to be beneficial to anyone else but companies that sell pain relievers since I have had to patronise them so often from when I began trying to achieve this. Below is my full code:
var http = require("http"), connect = require("./node_modules/connect"), static = require("./node_modules/serve-static"),
mysql = require("./node_modules/mysql"), bodyParser = require("./node_modules/body-parser").urlencoded({limit: "20kb", extended: true}), cookieSession = require("./node_modules/cookie-session"), app = connect(),
myPool = mysql.createPool({host: "localhost", port: 1717, user: "root", pass: "", multipleStatements: true, acquireTimeout: Number.POSITIVE_INFINITY})
.on("connection", function(connection) {console.log("new connect");});
myPool.getConnection(function(err, connection) {
if (err) {
throw err;
}
connection.query("CREATE DATABASE IF NOT EXISTS db DEFAULT CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci;USE db; CREATE TABLE IF NOT EXISTS users (email VARCHAR(150) NOT NULL , password VARCHAR(50) NOT NULL ) ENGINE = MyISAM;INSERT INTO users (email, password) VALUES ('email@gmail.com', '1717')",
function(err, result) {
if (err) throw err;
else {
console.log("created myPool");
connection.release();
}
});
});
app = app.use(bodyParser)
.use(cookieSession({secret: "mmay1717", maxAge: this.maxAge + (60 * 60 * 24 * 1000)})) /* prolong session by a day on each request*/
.use("/login", login)
.use(static(__dirname + "/public"));
function login (req, res, next) {
if (req.method == "POST") {
var params = req.body, resObj = {errText: "", sessName: ""}; console.log("received post request");
myPool.getConnection(function(err, connection) {
if (err) {
throw err;
}
connection.query("USE db; SELECT COUNT(*) FROM users WHERE email=? AND password=?", [params.email, params.pwd], function(err, numrows, rowInfo) {
if (err) {
throw err;
}
else if (numrows/*.length >= 1*/) {
console.log(numrows);
req.session.name = params.email.split("@")[0];
resObj.sessName = req.session.name;
res.end(JSON.stringify(resObj));
connection.release();
}
else {
resObj.errText = "Incorrect email or password";
res.end(JSON.stringify(resObj));
connection.release();
}
});
});
}
else
res.end("output login page here");
next();
}
http = http.createServer(app).listen(1717);