app.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // call the packages we need
  2. var express = require('express'); // call express
  3. var app = express(); // define our app using express
  4. var bodyParser = require('body-parser');
  5. var request = require("request");
  6. const edurouter = require('./pbl');
  7. const medurouter = require('./mysql-pbl');
  8. const mongo = require('./mongo');
  9. const weixin = require('./weixin');
  10. const cocoflow = require('./cocoflow');
  11. const szdjg = require('./szdjg');
  12. const cocostudy = require('./cocostudy');
  13. app.use('/api/cocostudy', cocostudy);
  14. const baoantoken = require('./baoantoken')
  15. const morgan = require('morgan');
  16. var path = require("path");
  17. var mongoose = require('mongoose');
  18. var session = require('express-session');
  19. const MongoStore = require('connect-mongo')(session);
  20. var port = "7003"; // set our port
  21. // const cors = require('cors')
  22. app.use(morgan('dev'));
  23. // configure app to use bodyParser()
  24. // this will let us get the data from a POST
  25. app.use(bodyParser.urlencoded({ extended: true, limit: '3mb' }));
  26. app.use(bodyParser.json({ limit: '3mb' }));
  27. // app.use(cors());
  28. //暂时全跨域
  29. app.use(function (req, res, next) {
  30. ///var allowedOrigins = [config.local.origin,'http://cocorobo.hk','http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'];
  31. var origin = req.headers.origin || "*";
  32. //if(allowedOrigins.indexOf(origin) > -1){
  33. res.setHeader('Access-Control-Allow-Origin', origin);
  34. //}
  35. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  36. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  37. res.header('Access-Control-Allow-Credentials', true);
  38. //修改程序信息与版本
  39. res.header('X-Powered-By', ' 3.2.1')
  40. //内容类型:如果是post请求必须指定这个属性
  41. res.header('Content-Type', 'application/json;charset=utf-8')
  42. next();
  43. });
  44. let local = 'mongodb://root:usestudio-1@172.16.9.240:27017?authSource=admin'
  45. // let local = 'mongodb://root:usestudio-1@183.36.26.8:11641/cocorobo?authSource=admin'
  46. // mongoose.connect(local, {
  47. // dbName: 'cocorobo',
  48. // useNewUrlParser: true,
  49. // useUnifiedTopology: true,
  50. // useCreateIndex: true,
  51. // useFindAndModify: true,
  52. // autoIndex: false, // Don't build indexes
  53. // reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
  54. // reconnectInterval: 50000, // Reconnect every 500ms
  55. // poolSize: 10000, // Maintain up to 10 socket connections
  56. // // If not connected, return errors immediately rather than waiting for reconnect
  57. // bufferMaxEntries: 0,
  58. // connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
  59. // socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
  60. // family: 4 // Use IPv4, skip trying IPv6
  61. // })
  62. // const db = mongoose.connection;
  63. // 配置session,使用MongoDB存储session数据
  64. // MongoDB连接字符串,使用与mongo.js中相同的连接方式
  65. // 注意:需要根据实际的MongoDB连接信息调整
  66. // app.use(session({
  67. // name: "cocorobo",
  68. // secret: 'cocorobo',
  69. // resave: true,
  70. // saveUninitialized: true,
  71. // cookie: {
  72. // sameSite: "None",
  73. // domain: '.cocorobo.cn',
  74. // httpOnly: false,
  75. // secure: false, // 根据实际情况调整,如果是HTTPS则设为true
  76. // maxAge: 24 * 60 * 60000 * 365
  77. // },
  78. // store: new MongoStore({
  79. // mongooseConnection: db,
  80. // touchAfter: 24 * 3600
  81. // }),
  82. // }));
  83. /*
  84. app.use(cors({
  85. origin:[`http:${config.local.origin}`,'http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'],
  86. methods:['GET','POST','PUT','DELETE'],
  87. credentials: true // enable set cookie
  88. }));
  89. */
  90. // all of our routes will be prefixed with /api
  91. app.use('/api/pbl', edurouter);
  92. app.use('/api/mysql-pbl', medurouter);
  93. app.use('/api/mongo', mongo);
  94. app.use('/api/weixin', weixin);
  95. app.use('/api/cocoflow', cocoflow);
  96. app.use('/api/szdjg', szdjg);
  97. app.use('/api/bat/getToken', async function (req, res, next) {
  98. let ticket = req.query.ticket
  99. await baoantoken.getToken(ticket, res)
  100. });
  101. // app.use('/game', game);
  102. app.all('/download', function (req, res, next) {
  103. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  104. request({
  105. url: req.body.url,
  106. method: "GET",
  107. encoding: null,
  108. headers: {
  109. 'Accept-Encoding': 'gzip, deflate'
  110. }
  111. },
  112. function (error, response, body) {
  113. if (!error && response.statusCode == 200) {
  114. res.setHeader('Content-Type', 'application/force-download');
  115. res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(req.body.url));
  116. res.setHeader('Content-Length', body.length);
  117. res.send(body);
  118. }
  119. });
  120. });
  121. // 捕获未处理的异常
  122. process.on('uncaughtException', (error) => {
  123. console.error("未处理的异常:", error);
  124. // 这里可以添加更多的错误处理逻辑,比如发送通知或记录日志
  125. });
  126. // 捕获未处理的Promise拒绝
  127. process.on('unhandledRejection', (reason, promise) => {
  128. console.error('未处理的Promise拒绝:', promise, '原因:', reason);
  129. // 这里可以添加更多的错误处理逻辑,比如发送通知或记录日志
  130. });
  131. // START THE SERVER
  132. // =============================================================================
  133. app.listen(port, '0.0.0.0');
  134. console.log('app happens on port ' + port);