Connection lost: The server closed the connection
With node_modules mysql using Node.js. I have installed npm's nodemon modules to continiously monitor my node.js application and restart the server when it crashese still when the sever crashes the application does not restart on its own.
After Searching lots of sites & implementing codes i have successfully resolved above error. For this I used mysqlpool. Code is below with description.
First of all you declare the connection like this :
var mysql = require('mysql');
var mysqlPool = mysql.createPool({
host : "localhost",
user : "username",
password: "password",
database :"databasename",
port:3306
});
Now your connection configuration has been successfully created & used like this :
var strQuery = "SELECT * FROM userdetails"; //here store sql query in strQuery variable.
mysqlPool.getConnection(function(err, connection) {
if(err) throw err;
connection.query(strQuery, function(err, rows) {
if(err) {
// if error occures goes here.
connection.end(); // if error occured closed the connection
console.error(err);
return;
}
//If no error here you get your query output.
console.log(rows[0].User_name); //retrieved username & print into console log or you can also retrived any rows like this.
connection.end();// After performing the operation then closed the connection.
});
});