-
Notifications
You must be signed in to change notification settings - Fork 28
/
router.js
153 lines (133 loc) · 3.5 KB
/
router.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const path = require('path');
const fs = require('fs');
const config = require('config');
const utils = require('./utils');
const view = require('../lib/view');
const dot = require('dot-object');
const extend = require('extend');
const express = require('express');
// eslint-disable-next-line
const router = express.Router({
caseSensitive: false,
strict: false,
});
const isProduction = config.get('server.production');
const isOffline = config.get('nitro.mode.offline');
const view404 = config.get('nitro.view404');
/**
* static routes
*/
router.use('/', express.static(`${config.get('nitro.basePath')}/public/`));
/**
* views
* @returns _nitro object
*/
function getNitroViewData(pageTitle, req) {
return {
_nitro: {
pageUrl: `${req.protocol}://${req.get('host')}${req.originalUrl}`,
pageTitle,
production: isProduction,
offline: isOffline,
}
}
}
function collectViewData(viewPath, defaultData, req) {
const data = {};
const tplPath = path.join(
config.get('nitro.basePath'),
config.get('nitro.viewDirectory'),
'/',
`${viewPath}.${config.get('nitro.viewFileExtension')}`
);
extend(true, data, defaultData);
if (!fs.existsSync(tplPath)) {
return false;
}
// collect data
const dataPath = path.join(
config.get('nitro.basePath'),
config.get('nitro.viewDataDirectory'),
'/',
`${viewPath}.json`
);
const customDataPath = req.query._data ? path.join(
config.get('nitro.basePath'),
config.get('nitro.viewDataDirectory'),
`/${req.query._data}.json`
) : false;
if (customDataPath && fs.existsSync(customDataPath)) {
extend(true, data, JSON.parse(fs.readFileSync(customDataPath, 'utf8')));
} else if (fs.existsSync(dataPath)) {
extend(true, data, JSON.parse(fs.readFileSync(dataPath, 'utf8')));
}
// handle query string parameters
if (Object.keys(req.query).length !== 0) {
// simple clone
const reqQuery = JSON.parse(JSON.stringify(req.query));
dot.object(reqQuery);
extend(true, data, reqQuery);
// save query for use in patterns
data._query = reqQuery;
}
// layout handling
if (data._layout) {
if (utils.layoutExists(data._layout)) {
data.layout = utils.getLayoutPath(data._layout);
}
}
if (!data.layout || !utils.layoutExists(utils.getLayoutName(data.layout))) {
// use default layout if present
if (utils.layoutExists(config.get('nitro.defaultLayout'))) {
data.layout = utils.getLayoutPath(config.get('nitro.defaultLayout'));
}
}
return {
tplPath,
data,
};
}
function getView(req, res, next) {
const tpl = req.params.view ? req.params.view.toLowerCase() : 'index';
const data = getNitroViewData(tpl, req);
const viewPathes = view.getViewCombinations(tpl);
let rendered = false;
extend(true, data, res.locals);
viewPathes.forEach((viewPath) => {
if (!rendered) {
const viewData = collectViewData(viewPath, data, req);
if (viewData) {
res.locals = viewData.data;
res.render(viewData.tplPath);
rendered = true;
}
}
});
if (!rendered) {
next();
}
}
router.get('/', getView);
router.get('/:view', getView);
// subpathes will be routed to the main view if it exists
router.get('/:view/*', getView);
/**
* everything else gets a 404
*/
router.use((req, res) => {
const data = getNitroViewData('404 - Not Found', req);
extend(true, data, res.locals);
const viewData = collectViewData(view404, data, req);
if (viewData) {
res.locals = viewData.data;
}
res.status(404);
res.render(view404, (err, html) => {
if (err) {
res.send('404 - Not Found');
}
res.send(html);
});
});
module.exports = router;