Authored by 郭成尧

'router'

  1 +/**
  2 + * 匹配subdomain
  3 + * @author: 杨延青<yanqing.yang@yoho.cn>
  4 + * @date: 2016/6/16
  5 + */
  6 +'use strict';
  7 +const querystring = require('querystring');
  8 +const helpers = global.yoho.helpers;
  9 +
  10 +module.exports = () => {
  11 + return (req, res, next) => {
  12 + if (req.subdomains.length) {
  13 + switch (req.subdomains[0]) {
  14 + case 'guang': // 逛
  15 + req.url = req.url.replace('/guang', '');
  16 + req.url = `/guang${req.url}`;
  17 + break;
  18 + case 'list': // list
  19 + req.url = `/product/index/index?${querystring.stringify(req.query)}`;
  20 + break;
  21 + case 'search': // search
  22 + req.url = `/product/search/list?${querystring.stringify(req.query)}`;
  23 + break;
  24 + case 'sale': // sale
  25 + res.redirect(301, helpers.urlFormat('/product/sale', req.query, 'default'));
  26 + return;
  27 + case 'new': // new.m.yohobuy.com 全部跳转到 m.yohobuy.com
  28 + res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
  29 + return;
  30 + case 'item': // item.m.yohobuy.com 全部跳转到 m.yohobuy.com
  31 + res.redirect(301, helpers.urlFormat(req.path, req.query, 'default'));
  32 + return;
  33 + default: // 其它(识别为品牌)
  34 + req.url = `/product/index/brand?domain=${req.subdomains[0]}`;
  35 + break;
  36 + }
  37 + }
  38 + next();
  39 + };
  40 +};