app.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var express = require('express');
  2. var path = require('path');
  3. var favicon = require('serve-favicon');
  4. var logger = require('morgan');
  5. var cookieParser = require('cookie-parser');
  6. var bodyParser = require('body-parser');
  7. var app = express();
  8. // view engine setup
  9. app.set('views', path.join(__dirname, 'views'));
  10. app.set('view engine', 'ejs');
  11. // uncomment after placing your favicon in /public
  12. //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  13. app.use(logger('dev'));
  14. app.use(bodyParser.json());
  15. app.use(bodyParser.urlencoded({
  16. extended: false
  17. }));
  18. app.use(cookieParser());
  19. app.use(express.static(path.join(__dirname, 'public')));
  20. //app.use('/', routes);
  21. //app.use('/users', users);
  22. //app.use('/menu', menu);
  23. var Router = require('./framework/lib/Router.js');
  24. var codeMap = require('./conf/code.inc.js');
  25. require('./conf/config.inc.js');
  26. var ResponseClass = require('./framework/lib/Response.js');
  27. var Doc = require('./framework/Doc.js');
  28. /**
  29. * @type {Response}
  30. */
  31. global['Response'] = new ResponseClass(codeMap);
  32. // catch 404 and forward to error handler
  33. app.use(function(req, res, next) {
  34. global['_req'] = req;
  35. global['_res'] = res;
  36. var objRouter = new Router(req.originalUrl);
  37. /**
  38. * @type {Controller}
  39. */
  40. var Controller = objRouter.getController();
  41. //doc
  42. if(req.query['doc'] && req.query['doc'] == 'module') {
  43. var doc = new Doc();
  44. var classInfos = doc.getClassInfos(__dirname + '/controllers');
  45. var docController = new Controller();
  46. docController.assign({classInfos : classInfos});
  47. docController.display('doc');
  48. return;
  49. }
  50. if (Controller) {
  51. var objController = new Controller();
  52. var action = objRouter.getFullActionName();
  53. // 先获取get参数,然后获取post参数
  54. var args = req.query || req.body || {};
  55. if (objController[action]) {
  56. objController[action](args);
  57. } else {
  58. var msg = 'can not find function ' + objRouter.getActionName();
  59. Response.error(CODE_NOT_EXIST_INTERFACE, msg);
  60. }
  61. } else {
  62. var msg = 'can not find class ' + objRouter.getControllerName();
  63. Response.error(CODE_NOT_EXIST_INTERFACE, msg);
  64. }
  65. });
  66. // error handlers
  67. // development error handler
  68. // will print stacktrace
  69. if (app.get('env') === 'development') {
  70. app.use(function(err, req, res, next) {
  71. res.status(err.status || 500);
  72. res.render('error', {
  73. message: err.message,
  74. error: err
  75. });
  76. });
  77. }
  78. // production error handler
  79. // no stacktraces leaked to user
  80. app.use(function(err, req, res, next) {
  81. res.status(err.status || 500);
  82. res.render('error', {
  83. message: err.message,
  84. error: {}
  85. });
  86. });
  87. module.exports = app;