Blame view

ccbikai authored
1 2 3 4 5
/**
 * yohobuy app
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/4/25
 */
周奇琪 authored
6
'use strict';
ccbikai authored
7
yyq authored
8 9 10
// if (process.env.USE_APM === '1' && process.env.NODE_ENV === 'production') {
//     require('oneapm');
// }
姜枫 authored
11
周奇琪 authored
12 13
const config = require('./config/common');
周少峰 authored
14 15 16 17 18 19 20
global.Promise = require('bluebird');

const yohoLib = require('yoho-node-lib');

// 全局注册library
yohoLib.global(config);
毕凯 authored
21
const express = require('express');
徐炜 authored
22
const compression = require('compression');
毕凯 authored
23 24 25 26
const path = require('path');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const favicon = require('serve-favicon');
htoooth authored
27 28
const _ = require('lodash');
const fp = require('lodash/fp');
htoooth authored
29
biao authored
30
const pkg = require('./package.json');
陈峰 authored
31
const hbsEvent = require('./config/hbsevent');
毕凯 authored
32
const app = express();
htoooth authored
33
const helpers = global.yoho.helpers;
周少峰 authored
34
周少峰 authored
35 36
app.disable('x-powered-by');
shijian authored
37 38
// tdk
global.yoho.redis = require('./doraemon/middleware/redis');
周少峰 authored
39
const tdk = require('./utils/getTDK');
shijian authored
40
htoooth authored
41 42
// NOTE: 这里修改了图片质量的参数
helpers.image = _.flow(helpers.image, fp.replace(/\/quality\/\d*$/, '/quality/90'));
周少峰 authored
43
htoooth authored
44
// NOTE:这里修改了参数的个数
htoooth authored
45
helpers.getUrlBySkc = skn => `//www.yohobuy.com/product/${skn}.html`;
htoooth authored
46
biao authored
47 48
global.middleware = path.resolve('./doraemon/middleware');
global.utils = path.resolve('./utils');
陈轩 authored
49
global.appRoot = path.resolve(__dirname);
biao authored
50
biao authored
51 52
// 向模板注入变量
app.locals.devEnv = app.get('env') === 'development';
yyq authored
53
app.locals.isProduction = app.get('env') === 'production';
biao authored
54 55
app.locals.version = pkg.version;
徐祁xuqi authored
56 57
// zookeeper
if (config.zookeeperServer) {
58
    require('yoho-zookeeper')(config.zookeeperServer, 'pc', app.locals.pc = {}, global.yoho.cache);
徐祁xuqi authored
59 60
}
yyq authored
61 62
app.enable('trust proxy');
yyq authored
63
app.set('subdomain offset', 2);
周少峰 authored
64
htoooth authored
65 66 67 68 69
app.use((req, res, next) => {
    req.isApmReport = _.get(req.app.locals, 'pc.open.bughd', false);
    next();
});
OF1706 authored
70 71 72
// 添加请求上下文
app.use(global.yoho.httpCtx());
周少峰 authored
73
app.use(global.yoho.hbs({
毕凯 authored
74 75
    extname: '.hbs',
    defaultLayout: 'layout',
周少峰 authored
76 77 78
    layoutsDir: path.join(__dirname, 'doraemon/views'),
    partialsDir: path.join(__dirname, 'doraemon/views/partial'),
    views: path.join(__dirname, 'doraemon/views'),
陈峰 authored
79 80
    helpers: Object.assign(global.yoho.helpers, require('./utils/helpers')),
    cb: hbsEvent.cb
毕凯 authored
81
}));
ccbikai authored
82
83
app.use(favicon(path.join(__dirname, '/favicon.ico')));
周少峰 authored
84
app.use(express.static(path.join(__dirname, 'static')));
ccbikai authored
85 86 87
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
徐炜 authored
88
app.use(compression());
周少峰 authored
89
htoooth authored
90
// session 设置
htoooth authored
91
require('./doraemon/middleware/yoho-session')(app);
周少峰 authored
92
93
app.use((req, res, next) => {
毕凯 authored
94 95
    req.user = {}; // 全局的用户数据
    req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
毕凯 authored
96
97 98 99 100
    if (!req.session) {
        req.session = {};
    }
yyq authored
101 102
    res.locals.nowYear = new Date().getFullYear();
103
    // req.app.locals = _.merge(req.app.locals, {
htoooth authored
104 105 106 107 108
    //    pc: {
    //        geetest: {
    //            validation: true
    //        }
    //    }
109
    // });
htoooth authored
110
毕凯 authored
111 112
    next();
});
周少峰 authored
113
shijian authored
114
// redis seo
周少峰 authored
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
app.use((req, res, next) => {
    if (!req.xhr) {
        tdk('url', `${req.hostname}${req.originalUrl}`, req).then(TDKObj =>{
            if (TDKObj[0]) {
                req.tdk = {
                    title: TDKObj[1],
                    keywords: TDKObj[2],
                    description: TDKObj[3]
                };
            }
            next();
        });
    } else {
        return next();
    }
});
周少峰 authored
131
姜枫 authored
132
const logger = global.yoho.logger;
biao authored
133
ccbikai authored
134
// dispatcher
毕凯 authored
135
try {
htoooth authored
136
    const setYohoData = require('./doraemon/middleware/set-yoho-data');
周少峰 authored
137
    const htaccess = require('./doraemon/middleware/htaccess');
yyq authored
138
    const subDomain = require('./doraemon/middleware/sub-domain');
yyq authored
139
    const mobileRefer = require('./doraemon/middleware/mobile-refer');
毕凯 authored
140
    const mobileCheck = require('./doraemon/middleware/mobile-check');
毕凯 authored
141 142
    const user = require('./doraemon/middleware/user');
    const seo = require('./doraemon/middleware/seo');
htoooth authored
143
    const errorHandler = require('./doraemon/middleware/error-handler');
biao authored
144
    const setPageInfo = require('./doraemon/middleware/set-pageinfo');
姜枫 authored
145
    const layoutTools = require('./doraemon/middleware/layout-tools');
姜枫 authored
146
    const pageCache = require('./doraemon/middleware/page-cache');
徐炜 authored
147
    const devtool = require('./doraemon/middleware/devtools');
htoooth authored
148
    const routeEncode = require('./doraemon/middleware/route-encode');
周少峰 authored
149
毕凯 authored
150
    // YOHO 前置中间件
htoooth authored
151
    app.use(setYohoData());
周少峰 authored
152
    app.use(htaccess());
yyq authored
153
    app.use(subDomain());
yyq authored
154
    app.use(mobileRefer());
毕凯 authored
155
    app.use(mobileCheck());
毕凯 authored
156
    app.use(user());
周少峰 authored
157
周少峰 authored
158 159 160 161
    // 请求限制中间件
    if (!app.locals.devEnv) {
        app.use(require('./doraemon/middleware/limiter'));
    }
周少峰 authored
162
毕凯 authored
163
    app.use(seo());
biao authored
164
    app.use(setPageInfo());
姜枫 authored
165
    app.use(layoutTools());
姜枫 authored
166
    app.use(pageCache());
htoooth authored
167
    app.use(routeEncode.md);
徐炜 authored
168 169 170 171

    if (app.locals.devEnv) {
        app.use(devtool());
    }
周少峰 authored
172
毕凯 authored
173 174
    require('./dispatch')(app);
htoooth authored
175
    app.all('*', errorHandler.notFound()); // 404
毕凯 authored
176 177

    // YOHO 后置中间件
htoooth authored
178
    app.use(errorHandler.serverError());
毕凯 authored
179 180 181
} catch (err) {
    logger.error(err);
}
ccbikai authored
182 183

// listener
biao authored
184
app.listen(config.port, function() {
毕凯 authored
185
    logger.info('yohobuy start');
陈峰 authored
186 187 188 189
    hbsEvent.event.on('hbs-complete', () => {
        process.send && process.send('ready');
        logger.info('hbs-all-complete');
    });
ccbikai authored
190
});