1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
app.js file
---------------
decalartion
var mysql = require('mysql');
var client = mysql.createClient({
  host : 'localhost',
  port : 3306,
  user: 'root',
  password : '',
  database : 'mydb',
  table : 'login'
});

app.get('/signup',routes.signup); //Sign up Form
app.post('/insert',routes.insert); // Sign up Value insert into mydb using sql query

----
index.js
----
Declaring again i dont  Why it is requiring it from app.js?? :(

ar mysql = require('mysql');
var HOST = 'localhost';
var PORT = 3306;
var MYSQL_USER = 'root';
var MYSQL_PASS = '';
var DATABASE = 'mydb';
var TABLE = 'login';

var client = mysql.createClient({
  host : HOST,
  port : PORT ,
  user: MYSQL_USER,
  password :MYSQL_PASS,
  database : DATABASE,
});
//Redirection to signup form working fine
 exports.signup = function(req,res){
  res.render('signup', {
    title : 'signup new user'
  });
}

//Function for inserting values into db give ERROR query is not the Method
exports.insert = function(req,res){
  mysql.query('insert into'+ TABLE + '(username,password)values("'+req.body.txtUser+'","'+req.body.txtPassword+'")',
  function(err,result){
    if(err){
      console.log("error in insertion:"+ err.message);
    }
    console.log('inserted'+result.affectedRows+'rows');
    console.log('Id inserted'+result.insertedId);
  } );
}