Blame view

app.js 3.76 KB
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
姜枫 authored
8 9 10 11
if (process.env.USE_APM === '1' && process.env.NODE_ENV === 'production') {
    require('oneapm');
}
周奇琪 authored
12 13
const config = require('./config/common');
姜枫 authored
14 15 16 17
global.Promise = require('bluebird');

const yohoLib = require('yoho-node-lib');
18 19
const express = require('express');
const path = require('path');
姜枫 authored
20
const compression = require('compression');
21 22 23
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const favicon = require('serve-favicon');
郭成尧 authored
24
const uuid = require('uuid');
姜枫 authored
25
26
const pkg = require('./package.json');
姜枫 authored
27
const session = require('client-sessions');
姜枫 authored
28
const _ = require('lodash');
ccbikai authored
29
30
const app = express();
31
毕凯 authored
32 33
// 向模板注入变量
app.locals.devEnv = app.get('env') === 'development';
ccbikai authored
34
app.locals.version = pkg.version;
毕凯 authored
35
徐祁xuqi authored
36 37 38 39 40
// zookeeper
if (config.zookeeperServer) {
    require('yoho-zookeeper')(config.zookeeperServer, 'wap', app.locals.wap = {});
}
郭成尧 authored
41
// 访问域名层级
42
app.set('subdomain offset', 3);
郭成尧 authored
43
ccbikai(👎🏻🍜) authored
44 45
app.set('etag', false);
46 47
// 全局注册library
yohoLib.global(config);
48
49
// 指定libray目录
ccbikai authored
50
global.utils = path.resolve('./utils');
51
姜枫 authored
52
const logger = global.yoho.logger;
htoooth authored
53
54
// 访问域名层级
郭成尧 authored
55
app.set('subdomain offset', 3);
姜枫 authored
56
app.use(global.yoho.hbs({
ccbikai authored
57 58
    extname: '.hbs',
    defaultLayout: 'layout',
姜枫 authored
59 60 61
    layoutsDir: path.join(__dirname, 'doraemon/views'),
    partialsDir: path.join(__dirname, 'doraemon/views/partial'),
    views: path.join(__dirname, 'doraemon/views'),
姜枫 authored
62
    helpers: _.assign(global.yoho.helpers, require('./utils/helpers'))
ccbikai authored
63
}));
ccbikai authored
64
姜枫 authored
65 66
app.use(global.yoho.middleware());
毕凯 authored
67 68
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
ccbikai authored
69
app.use(bodyParser.json());
姜枫 authored
70 71 72
app.use(bodyParser.urlencoded({
    extended: false
}));
ccbikai authored
73
app.use(cookieParser());
姜枫 authored
74
app.use(compression());
ccbikai authored
75
76
app.use(session({
姜枫 authored
77
    requestKey: 'session',
姜枫 authored
78
    cookieName: 'yohobuy_session_cookie',
姜枫 authored
79 80
    secret: '82dd7e724f2c6870472c89dfa43cf48d',
    domain: config.cookieDomain
81
}));
ccbikai authored
82
ccbikai authored
83
app.use((req, res, next) => {
ccbikai authored
84 85
    req.user = {}; // 全局的用户数据
    req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
86
    req.app.locals.wap = app.locals.wap; // zookeper对象赋值
姜枫 authored
87 88 89
    if (!req.session) {
        req.session = {};
    }
郭成尧 authored
90 91 92 93 94 95
    if (!req.session.id) {
        req.session.id = uuid.v4();
    }

    req.sessionID = req.session.id;
ccbikai authored
96 97 98
    next();
});
ccbikai authored
99
// dispatcher
htoooth authored
100
try {
ccbikai authored
101
    const user = require('./doraemon/middleware/user');
徐炜 authored
102
    const reqParamsFilter = require('./doraemon/middleware/req-params-filter');
103
    const subDomain = require('./doraemon/middleware/sub-domain');
郭成尧 authored
104
    const itemNameHandler = require('./doraemon/middleware/item-name-handler');
ccbikai authored
105
    const setYohoData = require('./doraemon/middleware/set-yoho-data');
ccbikai authored
106
    const errorHanlder = require('./doraemon/middleware/error-handler');
biao authored
107
    const setPageInfo = require('./doraemon/middleware/set-pageinfo');
Aiden Xu authored
108
    const devtools = require('./doraemon/middleware/devtools');
徐炜 authored
109
    const layoutTools = require('./doraemon/middleware/layout-tools');
110
    const seo = require('./doraemon/middleware/seo');
姜枫 authored
111
    const pageCache = require('./doraemon/middleware/page-cache');
ccbikai authored
112 113

    // YOHO 前置中间件
徐炜 authored
114
    app.use(reqParamsFilter());
115
    app.use(subDomain());
郭成尧 authored
116
    app.use(itemNameHandler);
ccbikai authored
117
    app.use(setYohoData());
ccbikai authored
118
    app.use(user());
119
    app.use(seo());
biao authored
120
    app.use(setPageInfo());
ccbikai authored
121
Aiden Xu authored
122 123 124 125
    if (app.locals.devEnv) {
        app.use(devtools());
    }
徐炜 authored
126 127
    app.use(layoutTools());
姜枫 authored
128
    app.use(pageCache());
htoooth authored
129
    require('./dispatch')(app);
ccbikai authored
130 131 132 133 134 135
    app.all('*', errorHanlder.notFound()); // 404

    // YOHO 后置中间件
    app.use(errorHanlder.serverError());
} catch (err) {
    logger.error(err);
htoooth authored
136
}
ccbikai authored
137 138

// listener
139
app.listen(config.port, function() {
ccbikai authored
140
    logger.info('yohobuy start');
ccbikai authored
141
});