-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
62 lines (53 loc) · 1.96 KB
/
routes.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
var express = require('express');
const app = require('./app');
var router = express.Router()
async function getData(req, res, next, controllerFile, pugFile) {
try {
const filepath = res.app.root + '/controllers/' + controllerFile + '.js';
const controller = require(filepath);
let data = await controller(req, res);
if (data === null || data === undefined) {
res.sendStatus(404);
} else if (typeof data === "object") {
if (data.json !== undefined) res.json(data.json);
else res.render(pugFile, data);
} else if (typeof data === "string") {
res.redirect(data);
}
} catch(e) {
console.log(e);
}
}
function addGet(route, controllerFile, pugFile, age=600) {
if (pugFile === undefined) pugFile = controllerFile;
router.get(route, (req, res, next) => {
res.set('Cache-Control', `public, max-age=${age}`);
getData(req, res, next, controllerFile, pugFile)
});
}
function addStatic(route, pugFile, title) {
if (title == undefined) title = pugFile;
router.get(route, (req, res, next) => {
res.render(pugFile, {title: title});
});
}
addGet('/', 'home');
addGet('/character/:id', 'character');
addGet('/corporation/:id', 'corporation', 'corporation', 1800);
addGet('/alliance/:id', 'alliance', 'alliance', 1800);
addStatic('/info/', 'info', '说明文档');
addStatic('/donate/', 'donate', '捐赠');
addStatic('/projects/', 'projects', '其它项目');
router.get('/autocomplete/', async function(req, res, next) {
// console.log(req.query.query)
const controller = require(res.app.root + '/controllers/autocomplete.js');
await controller(req, res);
});
// form routes
const submitController = require('./controllers/submit.js');
router.get('/submit', submitController.get);
router.post('/submit', submitController.post);
// router.post('/submit', (req, res, next) => {
// console.log(req)
// });
module.exports = router;