-
Notifications
You must be signed in to change notification settings - Fork 4
/
router.js
92 lines (89 loc) · 2.56 KB
/
router.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
'use strict';
var render = require('./render'),
quotes = require('./quotes'),
database = require('./database'),
cache = require('./cache'),
oembed = require('./oembed');
/**
* Routed paths. Paths are dictionaries containing a test regex and a
* renderer function.
*
* It's expected that exactly one regex will match a given path. Undefined
* behavior results if this assumption is violated.
*
* the render function is provided with 3 parameters:
* * uri (string): the URI provided by the client (path only)
* * request (Incomming Message): instance of:
* https://nodejs.org/api/http.html#http_http_incomingmessage
* * response (Server Response): instance of
* https://nodejs.org/api/http.html#http_class_http_serverresponse
*
* the render function is expected to handle all aspects of the server
* response itself.
*/
exports.paths = [{
path: /^\/static/,
renderer: render.serveStatic
}, {
path: /^\/templates/,
renderer: render.serveStatic
}, {
path: /^\/(index[.](html?|json|yml))?$/i,
renderer: render.renderIndex
}, {
path: /^\/scripts[.]js$/,
renderer: render.renderScripts
}, {
path: /^\/styles[.]css$/,
renderer: render.renderStyles
}, {
path: /^\/avatar\//,
renderer: quotes.serveAvatar
}, {
path: /^\/raw$/,
renderer: database.getRawData
}, {
path: /^\/oembed/,
renderer: oembed.getEmbedCode
}];
/**
* Additional paths available in dev mode.
*
* Dev mode is entered by setting the `SOCKDEV` environment variable to a
* truthy value
*/
if (process.env.SOCKDEV) {
exports.paths = exports.paths.concat([{
path: /^\/reset([.]html)?$/i,
renderer: function (uri, request, response) {
cache.buildCache(function () {
render.renderIndex(uri, request, response);
});
}
}, {
path: /^\/great([.]html)?$/i,
renderer: function (_, __, response) {
render.renderSample(100, response);
}
}, {
path: /^\/good([.]html)?$/i,
renderer: function (_, __, response) {
render.renderSample(1500, response);
}
}, {
path: /^\/ok([.]html)?$/i,
renderer: function (_, __, response) {
render.renderSample(2100, response);
}
}, {
path: /^\/bad([.]html)?$/i,
renderer: function (_, __, response) {
render.renderSample(3100, response);
}
}, {
path: /^\/offline([.]html)?$/i,
renderer: function (_, __, response) {
render.renderSample(21000, response);
}
}]);
}