-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (40 loc) · 1.31 KB
/
server.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
var app = require('express')(); // Express App include
var http = require('http').Server(app); // http server
var pg = require('pg'); // postgre include
var conString = "postgres://postgres:yellow@localhost:5432/testdb";
var bodyParser = require("body-parser"); // Body parser for fetch posted data
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
console.log('connected');
/* client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
}); */
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // Body parser use JSON data
app.get('/book',function(req,res){
console.log('get request');
var data = {
"error":1,
"Books":""
};
client.query("SELECT * from book",function(err, rows){
if(rows.length != 0){
data["error"] = 0;
data["Books"] = rows;
res.json(data);
}else{
data["Books"] = 'No books Found..';
res.json(data);
}
});
});
app.listen(3000);