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

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