Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (111 loc) · 3.69 KB
/
Copy pathapp.js
File metadata and controls
138 lines (111 loc) · 3.69 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
// node dependencies
var sys = require('sys');
var fs = require('fs');
// module dependencies
// filesystem references are post correct after running ndistro
var express = require('./modules/express');
var ejs = require('./modules/ejs');
// config
var app = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// Here we use the bodyDecoder middleware
// to parse urlencoded request bodies
// which populates req.body
app.use(express.bodyDecoder());
// Required by session
app.use(express.cookieDecoder());
// The methodOverride middleware allows us
// to set a hidden input of _method to an arbitrary
// HTTP method to support app.put(), app.del() etc
app.use(express.methodOverride());
// vars
boardNames = [];
boards = {};
initData = {"title": "foo", "modifier": "AN Other", "modified": "brand new!", "categories": ["Hot", "Not"], "cards": [{"name": "Paris", "categories": "Hot"}, {"name": "Paris Hilton", "categories": "Not"}]};
// timeout = 30000;
port = 3000;
// functions
function clone(obj){
if(obj == null || typeof(obj) != 'object') {
return obj;
}
var temp = obj.constructor(); // changed
for(var key in obj) {
temp[key] = clone(obj[key]);
}
return temp;
}
// Flush the boards json data from memory to the file system.
function persistData(file, json){
fs.writeFile(file, JSON.stringify(json), 'utf8', function(err){
if (err) { throw err; }
console.log('Saved data to ' + file);
});
}
// Load the stashed json data into memory.
function read(){
boards = JSON.parse(fs.readFileSync(__dirname + '/data/boards.json', 'utf8'));
boardNames = JSON.parse(fs.readFileSync(__dirname + '/data/boardNames.json', 'utf8'));
console.log('reading boards data');
console.log(JSON.stringify(boards));
console.log(JSON.stringify(boardNames));
}
// routes
app.get('/', function(req, res){
console.log('displaying homepage');
res.render('index.ejs');
});
app.get('/boards', function(req, res){
console.log('displaying boards');
res.render('boards.ejs', {
locals:{boards:boardNames}
});
});
app.post('/boards', function(req, res){
var boardName = req.body.name;
var board = boards[boardName];
if (undefined === board || null === board){
console.log('creating new board ' + boardName);
boardNames.push({"name":boardName});
var data = clone(initData);
data["title"] = boardName;
boards[boardName] = {"name":boardName, "data":data};
} else {
throw new Error('board already exists');
}
res.redirect('/boards/' + boardName);
});
// Deliver a board UI and include the initial data.
app.get('/boards/:id', function(req, res){
console.log('getting UI for board ' + req.params.id);
res.render('board.ejs', {
locals:{board:boards[req.params.id], title:req.params.id, data:JSON.stringify(boards[req.params.id].data)}
});
});
// Deliver the json data for a board.
app.get('/boards/:id/json', function(req, res){
console.log('getting JSON for board ' + req.params.id);
res.send(boards[req.params.id].data);
});
// Consume a blob of json, keep it in memory and also stash it on the disk.
app.put('/boards/:id/json', function(req, res){
console.log('putting JSON for board ' + req.params.id);
console.log(req.body.board);
boards[req.params.id].data = eval('(' + req.body.board + ')');
persistData("./data/boards.json", boards);
persistData("./data/boardNames.json", boardNames);
res.send('done');
});
// Delete a board from the data store.
app.del('/boards/:id', function(req, res){
boardName = req.params.id;
console.log('deleting ' + boardName);
boards[boardName] = null;
res.redirect('/boards');
});
// let's get started.
read();
app.listen(port);
console.log('Listening on port ' + port);