app.js 1.37 KB
/**
 * 应用入口
 * @author xuqi<qi.xu@yoho.cn>
 * @date: 2016/3/28
 */

var express = require('express'),
    path = require('path'),
    hbs = require('hbs'),
    bodyParser = require('body-parser');

var logger = require('morgan'),
    favicon = require('serve-favicon'),
    cookieParser = require('cookie-parser');

var app = express();

app.set('views', path.join(__dirname, 'views/action'));
app.set('view engine', 'hbs');
hbs.registerPartials(__dirname + '/views/partials');

app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req, res, next) {
    res.locals.devEnv = app.get('env') === 'development';
    next();
});

// 加载路由
app.use('/', require('./router'));

// 未知路由转入 404
// app.use(function(req, res) {
//     var err = new Error('Not Found');
//
//     err.status = 404;
//     res.send(404, err);
// });

// 捕获全局错误, 开发环境返回错误详情
app.use(function(err, req, res) {
    if (app.get('env') === 'development') {
        console.log(err);
    } else {
        err = {};
    }
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: err
    });
});


module.exports = app;