forked from dmrd/exponent-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (102 loc) · 3.21 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
const { parse } = require('url');
const next = require('next');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const compression = require('compression');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const port = 3000;
const handle = app.getRequestHandler();
const LATEST_VERSION = 'v' + require('./package.json').version;
const { WORKFLOW, DISTRIBUTION, EXPOKIT } = require('./transition/sections');
const CATEGORY_ALIASES = [
{ path: 'workflow', files: WORKFLOW },
{ path: 'distribution', files: DISTRIBUTION },
{ path: 'expokit', files: EXPOKIT },
];
const stripTrailingSlashAndExtensions = argument => {
if (argument.endsWith('.html')) {
argument = argument.replace('.html', '');
}
if (argument.endsWith('.md')) {
argument = argument.replace('.md', '');
}
if (argument.endsWith('/')) {
argument = argument.slice(0, -1);
}
return argument;
};
const mutateCategoryWithRedirectAlias = (category, post) => {
if (category.toLowerCase() === 'guides') {
for (let i = 0; i < CATEGORY_ALIASES.length; i++) {
const alias = CATEGORY_ALIASES[i];
if (alias.files.indexOf(post) > -1) {
category = alias.path;
}
}
}
return category;
};
app.prepare().then(() => {
const server = express();
server.use('/static', express.static('static'));
server.use(
bodyParser.urlencoded({
extended: false,
})
);
server.use(
cors({
origin: '*',
})
);
if (!dev) {
server.use(compression());
}
// NOTE(jim): Mutations have to line up with FS paths provided by mdjs.
server.get('/versions/:version', (req, res) => {
const { query } = parse(req.url, true);
let { version } = req.params;
version = stripTrailingSlashAndExtensions(version);
if (version === 'latest') {
version = LATEST_VERSION;
}
const updatedPath = `/versions/${version}`;
req.originalPath = updatedPath;
app.render(req, res, updatedPath, query);
});
server.get('/versions/:version/:category', (req, res) => {
const { query } = parse(req.url, true);
let { version, category } = req.params;
category = stripTrailingSlashAndExtensions(category);
if (version === 'latest') {
version = LATEST_VERSION;
}
const updatedPath = `/versions/${version}/${category}`;
req.originalPath = updatedPath;
app.render(req, res, updatedPath, query);
});
server.get('/versions/:version/:category/:post', (req, res) => {
const { query } = parse(req.url, true);
let { version, category, post } = req.params;
post = stripTrailingSlashAndExtensions(post);
if (version === 'latest') {
version = LATEST_VERSION;
}
category = mutateCategoryWithRedirectAlias(category, post);
const updatedPath = `/versions/${version}/${category}/${post}`;
req.originalPath = updatedPath;
app.render(req, res, updatedPath, query);
});
server.get('*', (req, res) => {
const { pathname, query } = parse(req.url, true);
app.render(req, res, pathname, query);
});
server.listen(port, err => {
if (err) {
throw err;
}
console.log(`The documentation server is running on localhost:${port}`);
});
});