app.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict'
  2. let express = require('express');
  3. let path = require('path');
  4. let favicon = require('serve-favicon');
  5. let logger = require('morgan');
  6. let cookieParser = require('cookie-parser');
  7. let bodyParser = require('body-parser');
  8. let domain = require('domain');
  9. let cluster = require('cluster');
  10. let app = express();
  11. // view engine setup
  12. app.set('views', path.join(__dirname, 'views'));
  13. app.set('view engine', 'ejs');
  14. // uncomment after placing your favicon in /public
  15. //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  16. app.use(logger('dev'));
  17. app.use(bodyParser.json());
  18. app.use(bodyParser.urlencoded({
  19. extended: false
  20. }));
  21. app.use(cookieParser());
  22. app.use(express.static(path.join(__dirname, 'public')));
  23. // 加载扩展函数
  24. require('./extensions/function_extend.js');
  25. let Router = require('./framework/lib/Router.js');
  26. let codeMap = require('./conf/code.inc.js');
  27. require('./conf/config.inc.js');
  28. let Response = require('./framework/lib/Response.js');
  29. global['AppErrors'] = require('./framework/lib/AppErrors.js');
  30. // catch 404 and forward to error handler
  31. app.use(function(req, res, next) {
  32. let objDomain = domain.create();
  33. objDomain.run(function () {
  34. main(req, res, next);
  35. });
  36. objDomain.on('error', function (err) {
  37. let objResponse = new Response(req, res, codeMap);
  38. handlerException(objResponse, err);
  39. });
  40. });
  41. function handlerException(objResponse, err) {
  42. try {
  43. if (err instanceof AppErrors.Interrupt) {
  44. // console.log('catch Interrupt.');
  45. } else if (err instanceof AppErrors.DbError) {
  46. objResponse.error(CODE_DB_ERROR, null, err.message, null);
  47. } else if (err instanceof AppErrors.RedisError) {
  48. objResponse.error(CODE_REDIS_ERROR, null, err.message, null);
  49. } else {
  50. // 对于未知情况,则返回位置错误
  51. objResponse.error(CODE_UNKNOW_ERROT, null, err.message, null);
  52. }
  53. } catch(ex) {
  54. console.error('catch(ex)');
  55. console.error(ex);
  56. }
  57. }
  58. function main(req, res, next) {
  59. let objRouter = new Router(req.originalUrl);
  60. /**
  61. * @type {Doc}
  62. */
  63. if(req.query['doc']) {
  64. objRouter.genDoc(req.query['doc']);
  65. } else {
  66. /**
  67. * @type {Controller}
  68. */
  69. let SomeController = objRouter.getController();
  70. let objResponse = new Response(req, res, codeMap);
  71. if (SomeController) {
  72. let objController = new SomeController(req, res);
  73. let action = objRouter.getFullActionName();
  74. // 先获取get参数,然后获取post参数
  75. let args = req.query || req.body || {};
  76. if (objController[action]) {
  77. objController[action](args);
  78. } else {
  79. let msg = 'can not find function ' + objRouter.getActionName();
  80. objResponse.error(CODE_NOT_EXIST_INTERFACE, msg);
  81. }
  82. } else {
  83. let msg = 'can not find class ' + objRouter.getControllerName();
  84. objResponse.error(CODE_NOT_EXIST_INTERFACE, msg);
  85. }
  86. }
  87. }
  88. // 捕捉全局异常
  89. // uncaughtException 避免程序崩溃
  90. process.on('uncaughtException', function(err) {
  91. let CallLog = require('./framework/lib/CallLog.js');
  92. // 记录访问日志
  93. let objCallLog = new CallLog(null);
  94. let msg = err.message && err.message.message;
  95. objCallLog.logSelfCall(CODE_UNKNOW_ERROT, msg);
  96. setTimeout(function() {
  97. process.exit(1);
  98. }, 100);
  99. });
  100. module.exports = app;