Authored by yyq

Merge branch 'feature/router' into feature/wp

... ... @@ -4,11 +4,13 @@
* @date: 2016/05/06
*/
var express = require('express'),
var _ = require('lodash'),
express = require('express'),
path = require('path'),
hbsEvent = require('../../config/hbsevent');
var app = express();
var router = require('./router');
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
... ... @@ -18,6 +20,33 @@ app.disable('x-powered-by');
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
if (router.rootRouter && router.rootRouter.length) {
let rootPath = '';
if (app.locals.rootPath && _.isString(app.locals.rootPath)) {
rootPath = app.locals.rootPath;
}
router.rootRouter.forEach(el => {
let fullPath = rootPath;
if (el.relatedPath && _.isString(el.relatedPath)) {
fullPath += el.relatedPath;
}
const pathArr = _.compact(fullPath.split('/'));
parent[el.method](el.path, (req, res, next) => {
Object.assign(res.locals, {
module: pathArr[0] || res.locals.module,
page: pathArr[1] || res.locals.page
});
return next();
}, ...el.args);
});
}
});
app.use(global.yoho.hbs({
... ... @@ -31,6 +60,6 @@ app.use(global.yoho.hbs({
}));
// router
app.use(require('./router'));
app.use(router);
module.exports = app;
... ...
... ... @@ -110,7 +110,7 @@ router.get(/\/global\/([\d]+)(.*)/, globalCtrl.detail);
// router.get(/\/global\/([\d]+)(.*)/, globalCtrl.detail); // 全球购商品详情页
// 搜索
router.get('/search', gbk2utf, search.index);
router.get('/search', '/search', gbk2utf, search.index);
router.get('/search/filter/brands', search.searchFilterBrands);
router.get('/search/suggest', search.suggest); // 搜索提示
router.get('/api/suggest', search.suggest4Old);
... ... @@ -124,9 +124,9 @@ router.get('/list/new', list.newWithChannel);
router.get('/list/new/:pathQs', paramParse, list.newWithChannel);
// 商品分类列表页
router.get('/list', gbk2utf, list.index);
router.get('/list', '/list', gbk2utf, list.index);
router.get('/list/index', gbk2utf, list.index);
router.get('/list/:pathQs', paramParse, gbk2utf, list.index);
router.get('/list/:pathQs', '/list/:pathQs', paramParse, gbk2utf, list.index);
// 品牌店铺
router.get('/index/about', list.brandAbout); // 品牌店铺介绍页
... ... @@ -139,9 +139,9 @@ router.get('/shop/couponsync', list.shopCouponSync);
router.get('/shop', shop.index); // 店铺首页
// router.get('/shop/:domain/:shopId.html', shop.index); // 店铺首页
router.get('/shop/:shopInfo.html', shop.index); // 店铺首页
router.get('/shop/:shopInfo', shop.list); // 店铺列表页
router.get('/shop/:shopInfo/:pathQs', paramParse, shop.list); // 店铺列表页
router.get('/shop/:shopInfo.html', '/shop/:shopInfo.html', shop.index); // 店铺首页
router.get('/shop/:shopInfo', '/shop/:shopInfo', shop.list); // 店铺列表页
router.get('/shop/:shopInfo/:pathQs', '/shop/:shopInfo/:pathQs', paramParse, shop.list); // 店铺列表页
// router.get('/shoplist', shop.list); // 店铺列表页
router.post('/shop/article', shop.article); // 店铺推荐文章
router.post('/shop/togglecollect', favorite.collectShop); // 店铺收藏
... ...
... ... @@ -4,7 +4,60 @@
* @date: 2016/4/27
*/
const _ = require('lodash');
const express = require('express');
const methods = require('methods');
const routerHold = (app) => {
app.use = ((appUse) => {
return (...args) => {
let rootPath = '';
const arg = args[0];
if (typeof arg !== 'function') {
rootPath = arg;
}
app.locals.rootPath = rootPath || '';
return appUse.call(app, ...args);
};
})(app.use);
express.Router = ((_router) => {
const baseRouter = _router();
return () => {
let router = _router();
methods.concat('all').forEach((method) => {
router[method] = (path, ...args) => {
const arg = args[0];
if (typeof arg !== 'function') {
args = _.drop(args);
router.rootRouter = router.rootRouter || [];
router.rootRouter.push({
path: arg,
relatedPath: path,
method: method,
args: args
});
}
return baseRouter[method].call(router, path, ...args);
};
});
return router;
};
})(express.Router);
};
module.exports = app => {
routerHold(app); // 拦截app.use && router
// 公共服务
app.use('/common', require('./apps/common'));
... ...
... ... @@ -209,11 +209,11 @@ module.exports = [
return '/product/index/about';
}
},
{
type: TYPE.rewrite,
origin: /^\/(list|search|new|sale|shop)((\/|\?)(.*)|())$/,
target: (req) => {
return `/product${req.url}`;
}
},
// {
// type: TYPE.rewrite,
// origin: /^\/(list|search|new|sale|shop)((\/|\?)(.*)|())$/,
// target: (req) => {
// return `/product${req.url}`;
// }
// },
];
... ...