-
Notifications
You must be signed in to change notification settings - Fork 60
/
express.js
127 lines (104 loc) · 3.33 KB
/
express.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var express = require('express'),
path = require('path'),
sr = require('./tools/static_require'),
fs = require('fs');
var HOST = '0.0.0.0',
PORT = 8000;
var examplesPath = path.join(__dirname, 'examples');
var existsSync = fs.existsSync || path.existsSync;
app = express.createServer();
app.set('views', path.join(examplesPath, 'views'));
app.get('/perf', sr.getAppHandler('Benchmark runner', '/perf/runner.js'));
app.get('/', function(req, res) {
res.redirect('/examples/');
});
app.get('/examples/', function(req, res) {
var exampleList = listExamples(examplesPath).map(function(name) {
var filePath = path.join(examplesPath, name),
code = getExampleCode(filePath);
return {
path: name + '/',
title: extractExampleTitle(code),
order: extractExampleOrder(code)
};
}).filter(function(a) {
return a.order > 0;
}).sort(function(a, b) {
return a.order*1 - b.order*1;
});
res.render('exampleList.jade', {
layout: false,
locals: { exampleList: exampleList }
});
});
app.get('/examples/:type/:example/', function(req, res) {
var filePath = path.join(examplesPath, req.param('type'), req.param('example')),
page = getExamplePage(filePath);
if (page) {
res.send(page);
} else {
var code = getExampleCode(filePath);
res.render('example.jade', {
layout: false,
locals: {
html: extractExampleHtml(code),
title: extractExampleTitle(code)
}
});
}
});
app.get('/test/*test.js', function(req, res){
res.sendfile(req.url.substring(1));
});
app.get('/test/qunit/*', function(req, res){
res.sendfile(req.url.substring(1));
});
app.get('*.js', sr.getHandler({
searchPaths: [
fs.realpathSync(path.join(__dirname, 'src'))
]
}));
app.get('/*', function(req, res) {
res.sendfile(req.param(0));
});
require('util').puts("Server at http://" + HOST + ":" + PORT + "/");
app.listen(PORT, HOST);
function getExamplePage (filePath) {
name = path.basename(filePath);
htmlPath = path.join(filePath, name + '.html');
if (existsSync(htmlPath)) {
return fs.readFileSync(htmlPath);
} else {
return false;
}
}
function listExamples (filePath) {
var result = [];
fs.readdirSync(filePath).forEach(function(name) {
if (existsSync(path.join(filePath, name, name + '.js'))) {
result.push(name);
} else if ( fs.statSync(path.join(filePath, name)).isDirectory() ) {
result = result.concat(listExamples(path.join(filePath, name)).map(function(subname) {
return path.join(name, subname);
}));
}
});
return result;
}
function getExampleCode (filePath) {
var name = path.basename(filePath),
jsPath = path.join(filePath, name + '.js');
return fs.readFileSync(jsPath, 'utf8');
}
function extractExampleHtml (code) {
var match = code.match(/@example_html((.|[\n\r])*)\*\//);
return match ? match[1] : '';
}
function extractExampleTitle (code) {
var match = code.match(/@example_title(.*)/);
return match ? match[1].trim() : 'Untitled';
}
function extractExampleOrder (code) {
var match = code.match(/@example_order(.*)/);
return match ? match[1].trim()*1 : 9e6;
}