-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
66 lines (50 loc) · 1.77 KB
/
server.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
var sys = require('sys'),
fs = require('fs'),
http = require('http'),
connect = require('connect'),
setup = require('./conf/setup');
// Data (will be backed by Redis soon)
// ============================================================================
// Documents
// ============================================================================
function document(app) {
// List
// --------------------------------------------------------------------------
app.get('/documents', function(req, res, next){
var body;
body = JSON.stringify(setup.documents);
res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': body.length
});
res.end(body, 'utf8');
});
// Show
// --------------------------------------------------------------------------
app.get('/documents/:id', function(req, res, next) {
if (!setup.documents[req.params.id])
return;
var host = setup.documents[req.params.id].host,
port = setup.documents[req.params.id].port || 80,
path = setup.documents[req.params.id].path;
// document knowledge is in the cloud
var service = http.createClient(port, host);
var request = service.request('GET', path, {'host': host});
request.end();
res.writeHead(200, {'Content-Type': 'text/plain'});
request.addListener('response', function (response) {
response.addListener('data', function (chunk) {
res.write(chunk);
});
response.addListener('end', function() {
res.end();
});
});
});
}
var server = connect.createServer(
connect.logger(),
connect.router(document),
connect.staticProvider(__dirname + '/public')
);
server.listen(3000);