-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexamples.js
46 lines (40 loc) · 1.43 KB
/
examples.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
fs = require('fs')
connect = require('connect')
coffee = require('coffee-script')
highlight = require('highlight').Highlight
connect()
.use(connect.logger('dev'))
.use(connect.static(__dirname + '/examples/ven'))
.use(connect.static(__dirname))
.use(connect.favicon())
.use(connect.directory(__dirname + '/examples', {
filter: function(file) {
return (/\.coffee$/).test(file)
}}))
.use(function(req, res, next) {
if(req.url.match(/\.(js|css|png|gif|svg)$/))
return next()
fs.readFile(__dirname + "/examples/layout.html", 'utf8', function(err, data) {
if(err) return next()
var out = "",
view = req.url,
source = __dirname + "/examples" + req.url.replace(/\.html/, '.coffee')
fs.readFile(source, 'utf8', function(err, coffeedata) {
if(err) return next()
var compiled = coffee.compile(coffeedata, {filename: source, bare: false})
res.writeHead(200, 'OK', {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'public, max-age=0'
})
out = data.replace('{{{SOURCE_ME}}}', highlight(coffeedata))
out = out.replace('{{{REPLACE_ME}}}', compiled)
res.end(out)
})
})
})
.use(function(req, res, next) {
res.writeHead(404, 'Not Found', {'content-type':'text/html'})
res.end("<h1>404 - Not Found</h1>")
})
.listen(9292);
console.log('Examples available at http://localhost:9292');