I am using NodeJs along with MySQL. I am using node-mysql after looking at MySQL with Node.js. Now the problem is I am trying to make db connection only once and using GETs inside it but it's not working. Code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret',
});
connection.connect(function(err) {
// connected! (unless `err` is set)
app.get('/hello',function(req,res){
console.log('Hello');
res.send('Hi');
});
});
But the above code is not working. It is not recognizing the route. But making a connection inside every get function works. I should be doing something wrong. Is there any way such that I can connect to the Db only once ?