-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerve-test.js
95 lines (72 loc) · 2.47 KB
/
nerve-test.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
var nerve = require('./nerve/nerve');
var querystring = require('./querystring/querystring');
var blog = require('./cassiterite');
var sys = require('sys');
// combo library
function Combo(callback) {
this.callback = callback;
this.items = 0;
this.results = [];
}
Combo.prototype = {
add: function () {
var self = this;
this.items++;
return function () {
self.check(self.items - 1, arguments);
};
},
check: function (id, arguments_in) {
this.results[id] = arguments_in;
this.items--;
if (this.items == 0) {
this.callback.call(this, this.results);
}
}
};
var get = nerve.get;
var post = nerve.post;
var getPostParams = function(req, callback) {
var body = '';
req.addListener('body', function(chunk){body += chunk;})
.addListener('complete', function(){
callback(unescape(body.replace(/\+/g,' ')));
});
};
var site ={
title: 'Small Node.js blog'
};
var hello = [
["/", function(req, res) {
blog.getPosts(0, 0, function(posts) {
var page = {'id': '0','title':'My small Node blog', 'posts': posts, 'pages':[]};
blog.render('page', res, page);
});
}],
[get("/write"), function(req, res) {
var page_text = '<h1>My small node blog</h1>';
page_text += '<h2>Write new blog entry</h2>';
page_text += '<form method="POST" action="/save">';
page_text += '<label for="title">Post title</label><br/><input type="text" name="title" /><br/>';
page_text += '<label for="body">Post body</label><br/><textarea cols="80" rows="24" name="body" >Enter post body</textarea>';
page_text += '<input type="submit" value="Save post" />';
page_text += '</form>';
blog.renderPage('Add content to blog', page_text, res);
}],
[post('/save'), function(req, res) {
// Save post to Tyrant
getPostParams(req, function(data) {
var post = querystring.parse(data);
post.time = (new Date()).getTime();
blog.savePost(post).addCallback(function(){
blog.renderPage('Post saved succesfully', '<p>Your post was saved succesfully, you can <a href="/add">add another</a> or <a href="/">return to main page</a>.</p>', res);
});
});
}],
[get(/^\/blog\/(\w+)$/), function(req, res, post_id) {
blog.getPost(post_id).addCallback(function(post) {
blog.render('post', res, post);
});
}]
];
nerve.create(hello).listen(8000);