app.js 1.9 KB
/**
 * yo app 入口
 * @author h1bomb
 */

var yo = require('yo.js'); //引用YO框架
var staticDir = require('./staticConfig').staticDir; //获取静态文件的不同环境配置
var auth = require('./mid/auth'); //认证中间件
var pageModule = require('./mid/pageModule'); //页面加载js中间件
var grayroute = require('./mid/grayroute'); //灰度界面中间件
var config = require('./config/common');
var util = require('./util/common');
var multipart = require('connect-multiparty');


var options = {
    //初始化yo的app
    appPath: __dirname + '/../', //应用所在目录
    tempExt: 'html', //模板引擎的扩展名
    envStatic: staticDir, //静态文件的环境配置
    session: {
        secret: 'yohobuy.shops', //session配置
        cookie: {
            secure: false,
            maxAge: 3600000
        } //session cookie失效时间1小时
    },
    interfaceDefConfig: {
        isJsonRaw: true //设置默认json字符串POST调用
    },
    loggers:config.loggers,               
    seStore:config.redis,
    port:config.port,
    beforeMid:function(app,mids) {//使用handlebars-layouts
        require('handlebars-layouts').register(mids.hbs.handlebars);
        app.use(multipart());
    },
    beforeCustMid: function(app, routers) {
        app.use(auth); //权限验证
        app.get('/apidoc', util.getInterfaceConfig(routers,config));
        app.use(grayroute(routers)); //灰度界面中间件
        app.use(pageModule); //客户端js模块加载
    },
    afterRender: function(res, str) { //在渲染完html后,加入模块调用的js

        if (res.locals.layout === false && res.locals.module) {
            str += '<script type="text/javascript" src="' + res.locals.module + '"></script>';
        }
        return str;
    }
}

if (config.logsFile) {
    options.logsFile = config.logsFile;
}

var app = yo(options);

require('./stub/routers')(app); //添加桩服务