sub-domain.js
1.54 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
/**
* 匹配subdomain
* @author: 杨延青<yanqing.yang@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const querystring = require('querystring');
const helpers = global.yoho.helpers;
module.exports = () => {
return (req, res, next) => {
if (req.subdomains.length) {
switch (req.subdomains[0]) {
case 'guang': // 逛
req.url = req.url.replace('/guang', '');
req.url = `/guang${req.url}`;
break;
case 'list': // list
req.url = `/product/index/index?${querystring.stringify(req.query)}`;
break;
case 'search': // search
req.url = `/product/search/list?${querystring.stringify(req.query)}`;
break;
case 'sale': // sale
res.redirect(301, helpers.urlFormat('/product/sale', req.query, 'default'));
return;
case 'new': // new.m.yohobuy.com 全部跳转到 m.yohobuy.com
res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
return;
case 'item': // item.m.yohobuy.com 全部跳转到 m.yohobuy.com
res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
return;
default: // 其它(识别为品牌)
req.url = `/product/index/brand?domain=${req.subdomains[0]}`;
break;
}
}
next();
};
};