You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log('You are now connected with mysql database...')
18
+
})
19
+
//end mysql connection
20
+
21
+
//start body-parser configuration
22
+
app.use(bodyParser.json());// to support JSON-encoded bodies
23
+
app.use(bodyParser.urlencoded({// to support URL-encoded bodies
24
+
extended: true
25
+
}));
26
+
//end body-parser configuration
27
+
28
+
//create app server
29
+
varserver=app.listen(3000,"127.0.0.1",function(){
30
+
31
+
varhost=server.address().address
32
+
varport=server.address().port
33
+
34
+
console.log("Example app listening at http://%s:%s",host,port)
35
+
36
+
});
37
+
38
+
//rest api to get all customers
39
+
app.get('/customer',function(req,res){
40
+
connection.query('select * from customer',function(error,results,fields){
41
+
if(error)throwerror;
42
+
res.end(JSON.stringify(results));
43
+
});
44
+
});
45
+
//rest api to get a single customer data
46
+
app.get('/customer/:id',function(req,res){
47
+
connection.query('select * from customers where Id=?',[req.params.id],function(error,results,fields){
48
+
if(error)throwerror;
49
+
res.end(JSON.stringify(results));
50
+
});
51
+
});
52
+
53
+
//rest api to create a new customer record into mysql database
54
+
app.post('/customer',function(req,res){
55
+
varparams=req.body;
56
+
console.log(params);
57
+
connection.query('INSERT INTO customer SET ?',params,function(error,results,fields){
58
+
if(error)throwerror;
59
+
res.end(JSON.stringify(results));
60
+
});
61
+
});
62
+
63
+
//rest api to update record into mysql database
64
+
app.put('/customer',function(req,res){
65
+
connection.query('UPDATE `customer` SET `Name`=?,`Address`=?,`Country`=?,`Phone`=? where `Id`=?',[req.body.Name,req.body.Address,req.body.Country,req.body.Phone,req.body.Id],function(error,results,fields){
66
+
if(error)throwerror;
67
+
res.end(JSON.stringify(results));
68
+
});
69
+
});
70
+
71
+
//rest api to delete record from mysql database
72
+
app.delete('/customer',function(req,res){
73
+
console.log(req.body);
74
+
connection.query('DELETE FROM `customer` WHERE `Id`=?',[req.body.Id],function(error,results,fields){
0 commit comments