app.js 1.6 KB

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