rewrite.js
1.08 KB
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
/**
* 路由重写
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2017/2/20
*/
'use strict';
const typeLib = require('../../config/type-lib');
const _ = require('lodash');
const resolve = (req, res, next) => {
let path = req.params[0],
params = {
channel: req.yoho.channel
};
if (!path) {
return next();
}
let conditions = path.replace('.html', '').split('-');
_.each(conditions, condition => {
if (typeLib.channels[condition]) {
params.channel = condition;
} else if (condition.indexOf('_') >= 0) {
let item = condition.split('_');
if (item.length === 2) {
params[item[0]] = item[1];
}
} else if (/\d+/.test(condition)) {
params.id = _.parseInt(condition);
}
});
req.yoho.channel = params.channel;
res.locals.channel = params.channel;
res.locals.pageChannel = { [params.channel]: true };
res.locals.setChannel = true;
Object.assign(req.query, params);
next();
};
module.exports = {
resolve
};