-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
134 lines (104 loc) · 3.54 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
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
128
129
130
131
132
133
134
var express = require('express');
var cookieParser = require('cookie-parser')
var app = express();
app.use(cookieParser());
var sass = require('node-sass');
var autoprefixer = require('autoprefixer');
var postcss = require('postcss');
var bodyParser = require('body-parser');
var fs = require('fs');
var serialize = require('./serialize');
var data = require('./data');
var matchers = require('./matchers');
app.use(bodyParser.text());
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', process.cwd() + '/src/');
app.locals.require = require;
app.use('/src/assets/scripts/bundle.js', function (req, res, next) {
var browserify = require('browserify')('./src/assets/scripts/app.js', {
debug: true
});
res.set('Content-Type', 'text/javascript');
browserify.bundle(function(error, buffer) {
console.log(`client bundle regenerated: ${buffer.length} bytes.`);
res.send(buffer);
});
});
app.get(/\/([^\s]+.(?:md|html))?$/, function(request, response) {
if (!request.params[0]) {
return serveIndex(request, response);
}
data.frames[0] = 'src/' + request.params[0].replace('html', 'md');
if (!data.frames.length) data.frames = ['src/demo.md'];
var frame = data.frames[0]
var frameContent = require('./readFile')(data.frames[0]).toString();
var titleMatch = matchers.getTitleFs(data.frames[0]);
var title = '';
if (titleMatch) {
title = titleMatch[1];
}
response.render('layout', {
bodyClass: data.bodyClass({
frame: frame,
singleColumn: matchers.singleColumn(frameContent)
}),
data: data,
prefix: process.env.prefix || 'src/',
header: '',
footer: '',
title: title,
frame: frame,
frameContent: frameContent,
published: true,
hasCode: matchers.hasCode(frameContent),
notrack: true,
requestStyle: request.query.style || request.cookies.style
});
});
function serveIndex(request, response) {
data.frames[0] = 'src/index.md'
var frame = data.frames[0]
var frameContent = require('./readFile')(data.frames[0]).toString();
var header = require('./readFile')('src/header', {raw: true}).toString();
var footer = require('./readFile')('src/footer', {raw: true}).toString();
response.render('layout', {
bodyClass: data.bodyClass({
frame: frame,
singleColumn: matchers.singleColumn(frameContent)
}),
index: true,
data: data,
prefix: process.env.prefix || 'src/',
header: header,
footer: footer,
share: '',
frameContent: frameContent,
hasCode: false,
published: true,
notrack: true,
requestStyle: request.query.style || request.cookies.style
});
}
function getStylesheet(request, response) {
var stylesheet = request.params.style || 'base';
response.set('Content-Type', 'text/css');
response.send(postcss([autoprefixer]).process(sass.renderSync({
file: process.cwd() + '/src/assets/stylesheets/' + stylesheet + '.scss'
}).css).css);
}
app.get('/stylesheets/:style.css', getStylesheet);
app.get('/src/assets/stylesheets/:style.css', getStylesheet);
app.put('/serialize', function(request, response) {
console.log(serialize(request.body));
response.sendStatus(200);
});
app.use(express.static(__dirname + '/src/assets'));
app.use('/src/assets', express.static(__dirname + '/src/assets'));
var invokeArgs = process.argv.slice(2).join(' ');
var listenPort = process.env.PORT ||
invokeArgs.match(/(?:port=)([0-9]+)\s?/) &&
invokeArgs.match(/(?:port=)([0-9]+)\s?/)[1] ||
3000;
app.listen(listenPort);
console.log('http server listening on', listenPort);