-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathurls.js
More file actions
77 lines (61 loc) · 1.78 KB
/
urls.js
File metadata and controls
77 lines (61 loc) · 1.78 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
var staticServing = require('./static-serving'),
interfaceServing = require('./interface/serve');
iapi = require('./iapi/urls'),
templating = require('./templating'),
crosspost = require('./crosspost/handlers'),
phone = require('./phone/handlers'),
metrics = require('./metrics/urls'),
fs = require('fs');
// Serves an individual file
var file = function(file, type) {
type = type || 'text/plain; charset=utf-8';
return function(req, res) {
res.writeHead(200, {'Content-Type': type});
fs.readFile(file, function(err, data) {
if (err) {
res.writeHead(500, {'Content-Type': 'text/html; charset=utf-8'});
res.end('Server error');
} else {
res.writeHead(200, {'Content-Type': type})
res.end(data);
}
});
};
};
var urls = {
//dummy handler that keeps us from clobbering croquet's urls
'^/croquet/': function() {},
//serve File objects from the db
'^/staticfile/': staticServing.serve,
//serve the api library
'^/api-library.js': interfaceServing.serve,
//delegate to the internal api
'^/iapi/': iapi.serve,
//crossposting urls
'^/crosspost': crosspost.serve,
//template testing
'^/template/': templating.serve,
//twilio stuff
'^/phone/': phone.serve,
//metrics
'^/metrics/': metrics.serve
};
var dispatch = function(req, res) {
// Special case for / for pingdom
if (req.url == '/') {
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
res.end('Hi');
return;
}
//try to dispatch
for (var r in urls) if (urls.hasOwnProperty(r)) {
if (req.url.match(r)) {
urls[r](req, res);
return;
}
}
//on failure, 404
res.writeHead(404, {'Content-Type': 'text/html; charset=utf-8'});
res.end('Not Found');
};
exports.dispatch = dispatch;