forked from totaljs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.js
More file actions
89 lines (66 loc) · 2.32 KB
/
default.js
File metadata and controls
89 lines (66 loc) · 2.32 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
exports.install = function(framework) {
// Documentation: http://docs.totaljs.com/Framework/#framework.route
framework.route('/', view_homepage);
framework.route('/contact/', view_contact);
framework.route('/products/', view_products);
framework.route('/products/{category}/', view_products);
framework.route('/products/{category}/{subcategory}/', view_products);
framework.route('/{category}/', view_homepage);
// this route has a lower priority and it will be executed when:
// url: /asterix/
// url: /asterix/bla/bla/bla/bla/
framework.route('/asterix/*', view_asterix);
// route: all txt files
// Documentation: http://docs.totaljs.com/Framework/#framework.file
// Try: http://127.0.0.4/test.txt
framework.file('All *.txt', static_txt);
// route: all jpg files
// all images will resized about 50%
// Documentation: http://docs.totaljs.com/Framework/#framework.file
// Try: http://127.0.0.4/header.jpg
framework.file('All *.jpg', static_jpg);
}
function static_txt(req, res, isValidation) {
if (isValidation)
return req.url.indexOf('.txt') !== -1;
// generate response
// this === framework
// Documentation: http://docs.totaljs.com/Framework/#framework.responsContent
this.responseContent(req, res, 200, 'Server time: ' + new Date().toString(), 'text/plain');
}
function static_jpg(req, res, isValidation) {
if (isValidation)
return req.url.indexOf('.jpg') !== -1;
// generate response
// this === framework
// Documentation: http://docs.totaljs.com/Framework/#framework.responseImage
this.responseImage(req, res, this.path.public(req.url), function (image) {
// image === FrameworkImage
// http://docs.totaljs.com/FrameworkImage/
image.resize('50%');
image.quality(80);
image.minify();
});
}
function view_homepage(category) {
category = category || '';
if (category.length > 0)
category = ' -> ' + category;
this.plain('homepage{0}'.format(category));
}
function view_contact() {
this.plain('contact');
}
function view_products(category, subcategory) {
category = category || '';
subcategory = subcategory || '';
if (category.length > 0)
category = ' -> ' + category;
if (subcategory.length > 0)
subcategory = ' -> ' + subcategory;
this.plain('products{0}{1}'.format(category, subcategory));
}
function view_asterix() {
var self = this;
self.plain('asterix -> ' + self.uri.pathname);
}