-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
213 lines (177 loc) · 5.7 KB
/
app.js
File metadata and controls
213 lines (177 loc) · 5.7 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Module dependencies.
*/
var express = require('express'),
jqtpl = require("jqtpl"),
app = module.exports = express.createServer(),
redis = require("redis"),
client = redis.createClient(null, null, {detect_buffers: true}),
specPost,
posts = [],
x = 1,
numOfPosts = 0,
arrayLength,
arrayOBJ = [],
indexPost,
indexPostOld = "",
clientLength = 10,
postObject;
var postSubject;
var postContent = "random text";
var postAuthor;
var timeVal = new Date;
var newTime = timeVal.getTime();
var listOfPosts = [];
// Configuration
//Setting up the server to handle certain views, makes this much more modular!
app.configure(function(){
app.set('views', __dirname + '/views');
app.set("view engine", "html");
app.register(".html", jqtpl.express);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
//The pseudo database collection that we start with
// stringfja = JSON.stringify([{ id:0, author: "jack", subject: "abc" },{ id:1, author: "mak", subject: "xyz"},{ id:3, author: "jack", subject: "a's sencond post" },{ id:2, author: "sas", subject: "sdfdsf" }]);
// client.set("All", stringfja);
//This is filling up the redis db with values
/*
stringfja = { author: "jack", subject: "abc", cont: "First set of content", timeVal:newTime };
client.set(stringfja.subject, JSON.stringify(stringfja));
stringfja = { author: "mak", subject: "xyz", cont: "Second set of content", timeVal:newTime};
client.set(stringfja.subject, JSON.stringify(stringfja));
var listOfPosts = ["abc", "xyz"];
*//*
key = "blog"
value = Array of posts
post = {id: 1, author: "test", subject: 'w00t'}
[{id: 1, author: "test", subject: 'w00t'},
{id: 2, author: "test", subject: 'w00t'},
{id: 3, author: "test", subject: 'w00t'},
{id: 4, author: "test", subject: 'w00t'},
{id: 5, author: "test", subject: 'w00t'}]
// /blog => index
client.get("blog", function ( err, res ) {
res.render("template", {posts: res})
})
// /create => create
app.post("/create", function (req, res)) {
client.get("blog", function (err, posts) {
posts.push({id: posts.length+1, author: JSON.stringify(req.body.user), subject: JSON.stringify(req.body.subject)})
client.set("blog", posts, function (err, result) {
res.redirect("/blog");
})
})
}
*/
// Will print `OK`
//This maintains the blog post at the specified index
app.get('/blog/:uid', function(req, res) {
//use the uid as a key for redis, return value to render specPost
var xqa = req.params.uid;
client.get(xqa, function (err, reply) {
specPost = JSON.parse(reply);
res.render("blog", {
post: specPost
})
});
});
function queryDB(query, resultReturn) {
resultReturn(query);
};
function renderResults(resultReturn, sendResults) {
sendResults(resultReturn);
};
//This maintains the all Blog Posts page!
app.get('/blog', function(req, res) {
xaz = 0;
var xyza = null;
collectDB(x, function() {
if(listOfPosts[0] ==null) {
res.render("allPosts");
}
for(x = 0; x < listOfPosts.length; x++) {
xyza = listOfPosts[x]
client.get(xyza, function (err, reply) {
if (err) {
console.log('error');
}
specPost = JSON.parse(reply);
posts.unshift(specPost);
respondClient(0, function(reqa, resa) {
if (xaz == listOfPosts.length-1) {
res.render("allPosts", { posts: posts });
posts = [];
} xaz++;
});
});
}
});
});
function collectDB(reqa, resa) {
setTimeout(100, resa(reqa));
}
function respondClient(reqb, resb) {
resb(reqb);
}
//receives the post request from create.html, adds the new post to
//the pseudo db and pushes back to allposts
app.post('/create', function(req, res) {
// globalVar++
var value = req.body.user;
valueAuth = value.author.toString();
valueSubj = value.subject.toString();
valueCont = value.cont.toString();
postObject = req.body.user;
var subject = postObject.subject;
var newTime = timeVal.getTime();
stringfja = {author: valueAuth, subject: valueSubj, cont: valueCont, timeVal:newTime };
listOfPosts.unshift(stringfja.subject)
client.set(stringfja.subject, JSON.stringify(stringfja), function ( err, result ) {
if (err) exit(1);
numOfPosts++;
res.redirect("/blog");
});
});
//Renders the new post page
app.get('/new', function(req, res) {
res.render("create");
});
//Add in an edit page
//This page will be accessed from the specifics page
//access the
app.get('/edit/:index', function(req, res) {
indexPost = req.params.index;
//indexPost = indexPost.subject;
indexPostOld = indexPost;
client.get(indexPost, function (err, reply) {
indexPost = JSON.parse(reply); // Will print `OK`
});
res.render("edit", {
post: indexPost
})
});
app.post('/editResult', function(req, res) {
var value = req.body.user;
valueAuth = value.author.toString();
valueSubj = value.subject.toString();
valueCont = value.cont.toString();
newTime = timeVal.getTime();
for(x = 0; x < listOfPosts.length; x++) {
stringfja = {author: JSON.stringify(valueAuth), subject: JSON.stringify(valueSubj), cont: valueCont, timeVal:newTime };
if(listOfPosts[x]==indexPostOld){
listOfPosts[x] = value.subject;
//value.time = "sometime";
client.set(valueSubj, JSON.stringify(value));
res.redirect('/blog');
}
}
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);