123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- var express = require('express');
- var path = require('path');
- var favicon = require('serve-favicon');
- var logger = require('morgan');
- var cookieParser = require('cookie-parser');
- var bodyParser = require('body-parser');
- var app = express();
- // view engine setup
- app.set('views', path.join(__dirname, 'views'));
- app.set('view engine', 'ejs');
- // uncomment after placing your favicon in /public
- //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('/', routes);
- //app.use('/users', users);
- //app.use('/menu', menu);
- var Router = require('./framework/lib/Router.js');
- var codeMap = require('./conf/code.inc.js');
- require('./conf/config.inc.js');
- var ResponseClass = require('./framework/lib/Response.js');
- var Doc = require('./framework/Doc.js');
- /**
- * @type {Response}
- */
- global['Response'] = new ResponseClass(codeMap);
- // catch 404 and forward to error handler
- app.use(function(req, res, next) {
- global['_req'] = req;
- global['_res'] = res;
- var objRouter = new Router(req.originalUrl);
- /**
- * @type {Controller}
- */
- var Controller = objRouter.getController();
- //doc
- if(req.query['doc'] && req.query['doc'] == 'module') {
- var doc = new Doc();
- var classInfos = doc.getClassInfos(__dirname + '/controllers');
- var docController = new Controller();
- docController.assign({classInfos : classInfos});
- docController.display('doc');
- return;
- }
- if (Controller) {
- var objController = new Controller();
- var action = objRouter.getFullActionName();
- // 先获取get参数,然后获取post参数
- var args = req.query || req.body || {};
- if (objController[action]) {
- objController[action](args);
- } else {
- var msg = 'can not find function ' + objRouter.getActionName();
- Response.error(CODE_NOT_EXIST_INTERFACE, msg);
- }
- } else {
- var msg = 'can not find class ' + objRouter.getControllerName();
- Response.error(CODE_NOT_EXIST_INTERFACE, msg);
- }
- });
- // error handlers
- // development error handler
- // will print stacktrace
- if (app.get('env') === 'development') {
- app.use(function(err, req, res, next) {
- res.status(err.status || 500);
- res.render('error', {
- message: err.message,
- error: err
- });
- });
- }
- // production error handler
- // no stacktraces leaked to user
- app.use(function(err, req, res, next) {
- res.status(err.status || 500);
- res.render('error', {
- message: err.message,
- error: {}
- });
- });
- module.exports = app;
|