-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
165 lines (149 loc) · 4.99 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*eslint-env node*/
var express = require('express'),
cfenv = require('cfenv'),
app = express(),
appEnv = cfenv.getAppEnv(),
bodyParser = require('body-parser'),
ejs = require('ejs'),
request = require('request');
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine','ejs');
var env = appEnv.services.cloudantNoSQLDB[0].credentials.url;
var db = 'task4db';
app.get('/',function (req, res) {
request.get({
url: env + "/"+db + "/_design/bears/_view/bears",
headers: {
'Content-Type': 'application/json'
}}, function (error, response, body) {
if ((error) || (!response.statusCode)) {
console.log("ERROR: Can't get target db's docs." +error);
}else if((response.statusCode < 300)) {
var d = JSON.parse(body).rows;
//res.json(d)
res.render('home',{bears:d});
}else {
console.log("We have no error, but status code is not valid: "+response.statusCode);
}
});
});
app.get('/bear/:id',function (req, res) {
var id = req.params.id;
request.get({
url: env + "/" + db + "/"+id,
headers: {
'Content-Type': 'application/json'
}},
function (error, response, body) {
if ((error) || (!response.statusCode)) {
console.log("ERROR: Can't get target db's docs." +error);
}else if((response.statusCode < 300)) {
var d = JSON.parse(body);
res.render('bear_details',{bear:d});
}else {
console.log("We have no error, but status code is not valid: "+response.statusCode);
res.render('notfound');
}
});
});
var rev = '';
//////DELETE BEAR//////
app.post('/bear/delete',function (req, res) {
var id = req.body.id;
var rev = req.body.rev;
var url = env + "/"+db + "/"+id+"?rev="+rev;
console.log("URL:"+url);
request.delete({
url: url,
headers: {
'Content-Type': 'application/json'
}},
function (error, response, body) {
if ((error) || (!response.statusCode)) {
console.log("ERROR: Can't get target db's docs." +error);
}else if((response.statusCode < 300)) {
res.redirect('/');
}else {
console.log("We have no error, but status code is not valid in delete: "+response.statusCode);
res.send(body);
}
});
});
app.get('/add',function (req, res) {
res.render('add');
});
app.post('/add',function (req, res) {
var bear = {
name:req.body.name,
description:req.body.description,
imgUrl:req.body.imgUrl,
create_date:Date.now()
}
var json = JSON.parse(JSON.stringify(bear));
request.post({
url:env + "/" + db, json:json},
function(err,httpResponse,body){
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
res.redirect('/');
});
//EDIT AND UPDATE
app.get('/bear/edit/:id',function (req, res) {
var id = req.params.id;
request.get({
url: env + "/"+db+"/"+id,
headers: {
'Content-Type': 'application/json'
}},
function (error, response, body) {
if ((error) || (!response.statusCode)) {
console.log("ERROR: Can't get target db's docs." +error);
}else if((response.statusCode < 300)) {
var d = JSON.parse(body);
res.render('edit',{bear:d});
}else {
console.log("We have no error, but status code is not valid: "+response.statusCode);
res.render('notfound');
}
});;
});
app.post('/bear/edit/',function (req, res) {
var id= req.body.id;
var rev = req.body.rev;
if(req.body.name && req.body.description && req.body.imgUrl){
var bear = {
name:req.body.name,
description:req.body.description,
imgUrl:req.body.imgUrl,
create_date:Date.now()
}
}else{
res.redirect('/');
}
var json = JSON.parse(JSON.stringify(bear));
request.put({
url:env + "/"+db +"/"+id+"?rev="+rev, json:json},
function(err,httpResponse,body){
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
res.redirect('/');
});
app.get('/about',function (req, res) {
res.render('about');
});
app.get('/:id',function (req, res) {
res.render('notfound');
});
//start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});