Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
var http = require('http'),
2
fs = require('fs'),
3
path = require('path'),
4
director = require('../../../lib/director'),
5
index;
6
7
fs.readFile('../html5-routes-harness.html', function (err, data) {
8
if (err) {
9
throw err;
10
}
11
12
index = data;
13
});
14
15
var CONTENT_TYPES = {
16
'.js' : 'text/javascript',
17
'.css' : 'text/css'
18
};
19
20
// Dummy file server
21
function fileServer(folder, file) {
22
var filepath = folder;
23
if (file) {
24
filepath += "/" + file;
25
}
26
27
// Special case
28
if (folder == "build") {
29
filepath = "../../../" + filepath;
30
} else {
31
filepath = "../" + filepath;
32
}
33
34
var res = this.res;
35
36
(fs.exists || path.exists)(filepath, function (exists) {
37
if (exists) {
38
fs.readFile(filepath, function (err, data) {
39
if (err) {
40
res.writeHead(404);
41
res.end();
42
}
43
44
res.writeHead(200, {'Content-Type': CONTENT_TYPES[path.extname(filepath)]});
45
res.end(data);
46
});
47
} else {
48
res.writeHead(404);
49
res.end();
50
}
51
});
52
}
53
54
var router = new director.http.Router({
55
'/files': {
56
'/:folder': {
57
'/:file': {
58
get: fileServer
59
},
60
61
get: fileServer
62
}
63
}
64
});
65
66
var server = http.createServer(function (req, res) {
67
router.dispatch(req, res, function (err) {
68
if (err && req.url != '/favicon.ico') {
69
// By default just reply with the index page
70
this.res.writeHead(200, {'Content-Type': 'text/html'});
71
this.res.end(index);
72
}
73
});
74
});
75
76
server.listen(8080);
77
78