-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (76 loc) · 2.32 KB
/
app.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
/**
* Module dependencies.
*/
var path = require('path');
var stylus = require('stylus');
var jade = require('jade');
var fs = require('fs');
var url = require('url');
var ecstatic = require('ecstatic');
var md = require('marked');
var app = require('express')();
var quotes = require('./pretty.json');
// local files as strings
var locals = {
'toggleTheme': require('./scripts/local-storage.mm')(),
'.env': require('./raw/.env')(),
'.foreman': require('./raw/.foreman')(),
'Makefile': require('./raw/Makefile')(),
'Procfile.dev': require('./raw/Procfile.dev')(),
'app.js': require('./raw/app.js')(),
'header.jade': require('./raw/public/partials/header.jade')(),
'index.jade': require('./raw/views/index.jade')(),
'index.js': require('./raw/public/javascripts/index.js')(),
'index.styl': require('./raw/public/stylesheets/index.styl')(),
'routes.js': require('./raw/public/javascripts/routes.js')(),
'component.json': JSON.stringify(require('./raw/component.json'), null, ' '),
'package.json': JSON.stringify(require('./raw/package.json'), null, ' ')
};
// stylus
app.use(function(req, res, next){
req.url = req.url.replace(/\.styl$/, '.css');
next();
});
app.use(function(req, res, next){
app.use(stylus.middleware({ src: __dirname }));
next();
});
// get random quotes
app.use(function(req, res, next) {
locals.quotes = res.locals.quotes = randomQuote(100);
next();
});
var fn;
// jade
app.use(function(req, res, next){
// if (req.url !== '/' && !req.url.match(/\.jade$/)) return next();
if (!req.url.match(/\.jade$/)) return next();
var file = path.join(__dirname, 'index.jade');
// if (typeof fn === 'function') return finish(res);
fs.readFile(file, 'utf8', function(err, str){
if (err) return next(err);
try {
fn = jade.compile(str, { filename: file, pretty: false });
finish(res);
} catch (err) {
next(err);
}
});
});
// static files
app.use(ecstatic({root: __dirname}));
function finish(res) {
var str = fn(locals);
res.setHeader('Content-Type', 'text/html');
res.setHeader('Content-Length', Buffer.byteLength(str));
res.setHeader('Content-Type', 'text/html');
res.end(str);
}
function randomQuote(l){
var arr = [];
for (var i = 0; i < l; i++) {
arr[i] = quotes[Math.floor(Math.random()*quotes.length)];
}
return arr;
}
module.exports = app;