What does the connection.connect() do in node-mysql
library?
My code looks like this
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'ap-cdbr-azure-east-a.cloudapp.net',
user : '<user>',
password : '<password>',
database : '<database>',
});
connection.connect();
Later I run the query.
Even if I remove connection.connect()
the code works as is. Some examples have it, some don't. What is the specific purpose for connect() ?
The recommended way to establish a connection is this:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret',
});
connection.connect(function(err) {
// connected! (unless `err` is set)
});
However, a connection can also be implicitly established by invoking a query:
var mysql = require('mysql');
var connection = mysql.createConnection(...);
connection.query('SELECT 1', function(err, rows) {
// connected! (unless `err` is set)
});
Depending on how you like to handle your errors, either method may be appropriate. Any type of connection error (handshake or network) is considered a fatal error.
Courtesy: https://github.com/felixge/node-mysql