app.js 1.08 KB
/**
 * yoho-activity app
 * @author: leo<qi.li@yoho.cn>
 * @date: 2017/6/23
 */
'use strict';

const path = require('path');
const utils = require('./utils');
const express = require('express');
const config = require('./config');
const bluebird = require('bluebird');
const ynLib = require('yoho-node-lib');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

let logger;
const app = express();

// 全局注册library
ynLib.global(config);
logger = global.yoho.logger;

global.Promise = bluebird;
global.yoho.utils = utils;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(cookieParser());
app.use(global.yoho.httpCtx());

app.use((req, res, next) => {
    req.user = {}; // 全局的用户数据
    next();
});


const middleware = require('./middleware');

try {

    // 路由分发
    require('./dispatch')(app);

    // 错误处理
    app.use(middleware.error);

} catch (err) {
    logger.error(err);
}

app.listen(config.port, function() {
    logger.info('yoho-activity-node started successfully!');
});