Step 1-> create simple your project folde "testapp" under below directory "www"
C:\Bikash\codetest\wamp\www\testapp
create app.js
var mysql = require("mysql");
// First you need to create a connection to the db
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database : 'test',
});
con.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
function getColour(uid, roomCount, callback)
{
con.query('SELECT u_name FROM user WHERE u_id = ?', [roomCount], function(err, result)
{
if (err)
callback(err,null);
else
callback(null,result[0].u_name);
});
}
//call Fn for db query with callback
getColour("2",1, function(err,data){
if (err) {
// error handling code goes here
console.log("ERROR : ",err);
} else {
// code to execute on data retrieval
console.log("result from db is : ",data);
}
});
con.end(function(err) {
console.log('Connection has been closed');
});
Step2->open cmd
Step4->C:\Bikash\codetest\wamp\www\testapp>npm install mysql
step5->run like : C:\Bikash\codetest\wamp\www\test>node app.js