-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (129 loc) · 4.44 KB
/
app.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var pg = require("pg")
var express = require("express")
var bodyparser = require("body-parser")
var cors = require('cors')
var app = express()
var multer = require('multer');
//app.use(cors());
//app.use(multer());
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
//var port = 5433;
var data;
//var data = [{"name":"Basava", "Sex": "Male"},{"name":"Manoj", "Sex":"Male"},{"name":"Sai","Sex":"Male"}];
var writeResults = function(req,res){
console.log("Write Results");
res.writeHead(200, {'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Headers': 'X-Requested-With, accept, content-type',
'Access-Control-Allow-Methods': 'GET'});
res.write(data);
res.end();
};
var getRecords = function(req,res,callback){
console.log("Get Records");
var conString = "pg://postgres:12345@localhost:5432/TestDB";
var client = new pg.Client(conString);
client.connect();
var query = client.query('select id,fname,lname,0 as "Dbupdate" from Student order by id');
query.on("row", function (row, result) {
result.addRow(row);
});
query.on("end", function (result) {
client.end();
data = JSON.stringify(result.rows, null, " ");
//console.log(data);
callback(req,res);
});
};
var updateRecords = function(req,res){
var conString = "pg://postgres:12345@localhost:5432/TestDB";
var client = new pg.Client(conString);
client.connect();
//console.log(req.body.fname);
//var query = client.query("update \"Student\" set fname = '"+ req.fname +"' where id = "+req.id);
client.query("update Student set fname = $1 where id = $2",[req.body.fname,req.body.id],function(err,result){
if(err) {
return console.error('error running update query', err);
}
client.end();
});
// res.writeHead(200, {'Content-Type': 'text/plain'});
res.writeHead(200, {'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Headers': 'X-Requested-With, accept, content-type',
'Access-Control-Allow-Methods': 'POST'});
res.write("Updated record to " + req.body.fname);
res.end();
}
var insertRecords = function(req,res)
{
console.log("In insert");
// Connect to DB
var conString = "pg://postgres:12345@localhost:5432/TestDB";
var client = new pg.Client(conString);
client.connect();
client.query("INSERT INTO Student(fname, lname) values($1, $2)", [req.body.fname, req.body.lname],function(err,result){
if(err) {
return console.error('error running Insert query', err);
}
client.end();
});
// res.writeHead(200, {'Content-Type': 'text/plain'});
res.writeHead(200, {'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Headers': 'X-Requested-With, accept, content-type',
'Access-Control-Allow-Methods': 'POST'});
res.write("1 record is inserted.\n");
res.end();
console.log("Inserted 1 records");
}
var deleteRecords = function(req,res){
console.log("In Delete");
// Connect to DB
var conString = "pg://postgres:12345@localhost:5432/TestDB";
var client = new pg.Client(conString);
client.connect();
//Delete record
client.query("DELETE FROM Student WHERE id = $1",[req.body.id],function(err,result){
if(err) {
return console.error('error running Insert query', err);
}
client.end();
});
// res.writeHead(200, {'Content-Type': 'text/plain'});
res.writeHead(200, {'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Headers': 'X-Requested-With, accept, content-type',
'Access-Control-Allow-Methods': 'POST'});
res.write("Deleted record where id = "+ req.body.id);
res.end();
console.log("Deleted record");
}
app.get('/',function(req,res){
getRecords(req,res,writeResults);
})
app.post('/Update',function(req,res){
console.log(req.body.fname);
console.log(req.body.lname);
updateRecords(req,res);
})
app.post('/Insert', function(req,res){
console.log(req.body.fname);
console.log(req.body.lname);
console.log(req.body);
insertRecords(req,res);
})
app.post('/del',function(req,res){
console.log(req.body.id);
deleteRecords(req,res);
})
var server = app.listen(5433,function(){
var host = "localhost"
var port = "5433"
});