mkdir nodejsmysqlconnectiontutorial
974 cd nodejsmysqlconnectiontutorial/
975 npm init
976 npm install –save express@latest mysql@latest
vi script.js
var express = require('express');
var mysql = require('mysql');
var app = express();
// var connection = mysql.createConnection({
var connection = mysql.createPool({
// properties..
connectiononLimit:50,
host:'localhost',
user:'root',
password:'611',
database:'products'
});
// connection.connect(function(error){
// //callback
// if(!!error){
// console.log('error');
// } else{
// console.log('connected');
// }
// });
app.get('/',function(req,res){
// //about mysql
// connection.query("select * from users",function(error,rows,fields){
// // callback
// if(!!error){
// console.log('error in the query');
// } else{
// console.log('successfull in the query');
// console.log(rows[0].email);
// res.send('hello'+ rows[0].email);
// // res.send({rows});
// }
connection.getConnection(function(error,tempCount){
if(!!error){
tempCount.release();
console.log('Error');
}else{
console.log('connected!');
tempCount.query('select * from users',function(error,rows,fields){
tempCount.release();
if(!!error){
console.log('error in the query');
} else{
res.json(rows);
// res.send('hello'+ rows[0].email);
}
});
}
});
})
app.listen(1337);