sub-domain.js
3.46 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
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
/**
* 匹配subdomain
* @author: 杨延青<yanqing.yang@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
const _ = require('lodash');
const qs = require('querystring');
module.exports = () => {
return (req, res, next) => {
if (req.subdomains.length) {
switch (req.subdomains[0]) {
case 'www': // 主站
case 'cdnsrcwww': // 主站的回源地址
case 'shop': // 商家入驻
case 'new':
case 'item':// 商品详情页
break;
case 'guang': // 逛
case 'cdnsrcguang': // 逛CDN回源解析
{ // eslint-disable-line
let guangReg = /^\/guang/;
let guangDetailReg = /.html$/;
if (guangDetailReg.test(req.path)) {
// req.url = '/guang/detail' + req.url;
/\/([\d]*).html(\?)?(.*)/.exec(req.url);
req.url = '/guang/info/index?id=' + RegExp.$1;
if (RegExp.$3) {
req.url += '&' + RegExp.$3;
}
req.query.id = RegExp.$1;
} else if (!guangReg.test(req.path)) {
req.url = '/guang' + req.url;
}
break;
}
case 'search': // 搜索
{ // eslint-disable-line
let searchReg = /^\/product\//;
if (!searchReg.test(req.path)) {
if (req.path === '/api/suggest') {
req.url = '/product/api/suggest';
} else {
req.url = '/product/search/index';
}
}
break;
}
case 'list': // 商品列表
case 'cdnsrclist': // 商品列表CDN回源解析
{ // eslint-disable-line
if (!req.path || req.path === '/') {
req.url = '/product/list/index';
} else if (req.path === '/new') {
req.url = '/product/list/new';
}
break;
}
case 'sale': // 促销
case 'cdnsrcsale': // 促销CDN回源解析
{ // eslint-disable-line
if (!req.path || req.path === '/') {
req.url = '/product/sale/special/detail';
}
break;
}
default: // 其它(识别为品牌)
{ // eslint-disable-line
let queryString = (function() {
if (!_.isEmpty(req.query)) {
return '&' + qs.stringify(req.query);
} else {
return '';
}
}());
req.query.domain = req.subdomains[0];
if (!req.path || req.path === '/') {
req.url = `/product/index/brand?domain=${req.subdomains[0]}${queryString}`;
} else if (req.path === '/about') {
req.url = `/product/index/brand?domain=${req.subdomains[0]}${queryString}`;
}
break;
}
}
}
next();
};
};