app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * express Main
  3. */
  4. console.time("服务启动用时")
  5. const u_config = require('../config/systemConfig');
  6. const express = require('express');
  7. const APP = express();
  8. //系统初始化入口
  9. const startServer = require('./server/InitServer.js')
  10. /**日志框架*/
  11. var logger = require('morgan');
  12. APP.use(logger('dev'));
  13. /**解析post body各种格式的请求体*/
  14. let bodyParser = require('body-parser');
  15. // 解析 application/json
  16. APP.use(bodyParser.json());
  17. // 解析 url编码
  18. APP.use(bodyParser.urlencoded({ extended: true }));
  19. /**设置跨域访问 方式1*/
  20. APP.all('*', function (req, res, next) {
  21. res.header("Access-Control-Allow-Origin", "*");
  22. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  23. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  24. res.header("X-Powered-By", ' 3.2.1')
  25. // res.header("Content-Type", "application/json;charset=utf-8");
  26. res.header("Cache-Control", "no-cache, no-store, must-revalidate");
  27. res.header("Pragma", "no-cache");
  28. res.header("Expires", 0);
  29. next();
  30. });
  31. /**设置跨域访问 方式2 */
  32. const cors = require('cors');
  33. APP.use(cors())
  34. /**服务器端禁用缓存 */
  35. APP.disable('etag');
  36. /**异常处理 */
  37. process.on('uncaughtException', function (err) {
  38. console.log("uninpho错误提示:" + err);
  39. console.log("uninpho错误提示:" + err.stack)
  40. });
  41. //服务初始化入口
  42. startServer(APP);
  43. /**启动服务-端口-IP */
  44. APP.listen(u_config.base.PORT, u_config.base.IP, function () {
  45. console.log('\x1B[34m%s\x1B[0m', `服务地址: http://${ u_config.base.IP}:${u_config.base.PORT}/`);
  46. console.timeEnd("服务启动用时");
  47. });