sub-domain.js 2.16 KB
/**
 * 匹配subdomain
 * @author: 杨延青<yanqing.yang@yoho.cn>
 * @date: 2016/6/16
 */
'use strict';

module.exports = () => {
    return (req, res, next) => {
        let searchReg = /^\/product\//,
            guangReg = /^\/guang/,
            guangDetailReg = /.html$/;

        if (req.subdomains.length) {
            switch (req.subdomains[0]) {
                case 'www': // 主站
                case 'new': // 原新版
                case 'shop': // 商家入驻
                case 'item':// 商品详情页
                    break;
                case 'guang': // 逛
                    if (guangDetailReg.test(req.path)) {
                        req.url = '/guang/detail' + req.url;
                    } else if (!guangReg.test(req.path)) {
                        req.url = '/guang' + req.url;
                    }
                    break;
                case 'search': // 搜索
                    if (!searchReg.test(req.path)) {
                        if (req.path === '/api/suggest') {
                            req.url = '/product/api/suggest';
                        } else {
                            req.url = '/product/search/index';
                        }
                    }
                    break;
                case 'list': // 商品列表
                    if (!req.path || req.path === '/') {
                        req.url = '/product/list/index';
                    } else if (req.path === '/new') {
                        req.url = '/product/list/new';
                    }
                    break;
                case 'sale': // 促销
                    if (!req.path || req.path === '/') {
                        req.url = '/product/sale/special/detail';
                    }
                    break;
                default: // 其它(识别为品牌)
                    if (!req.path || req.path === '/') {
                        req.url = `/product/brand?domain=${req.subdomains[0]}`;
                    } else if (req.path === '/about') {
                        req.url = `/product/about?domain=${req.subdomains[0]}`;
                    }
                    break;
            }
        }
        next();
    };
};