app.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if (req.method === 'OPTIONS') {
  43. return res.sendStatus(204);
  44. }
  45. next();
  46. });
  47. let local = 'mongodb://root:usestudio-1@172.16.9.240:27017?authSource=admin'
  48. // let local = 'mongodb://root:usestudio-1@183.36.26.8:11641/cocorobo?authSource=admin'
  49. mongoose.connect(local, {
  50. dbName: 'cocorobo',
  51. useNewUrlParser: true,
  52. useUnifiedTopology: true,
  53. useCreateIndex: true,
  54. useFindAndModify: true,
  55. autoIndex: false, // Don't build indexes
  56. reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
  57. reconnectInterval: 50000, // Reconnect every 500ms
  58. poolSize: 10000, // Maintain up to 10 socket connections
  59. // If not connected, return errors immediately rather than waiting for reconnect
  60. bufferMaxEntries: 0,
  61. connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
  62. socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
  63. family: 4 // Use IPv4, skip trying IPv6
  64. })
  65. const db = mongoose.connection;
  66. // 配置session,使用MongoDB存储session数据
  67. // MongoDB连接字符串,使用与mongo.js中相同的连接方式
  68. // 注意:需要根据实际的MongoDB连接信息调整
  69. app.use(session({
  70. name: "cocorobo",
  71. secret: 'cocorobo',
  72. resave: true,
  73. saveUninitialized: true,
  74. cookie: {
  75. sameSite: "None",
  76. domain: '.cocorobo.cn',
  77. httpOnly: false,
  78. secure: false, // 根据实际情况调整,如果是HTTPS则设为true
  79. maxAge: 24 * 60 * 60000 * 365
  80. },
  81. store: new MongoStore({
  82. mongooseConnection: db,
  83. touchAfter: 24 * 3600
  84. }),
  85. }));
  86. /*
  87. app.use(cors({
  88. origin:[`http:${config.local.origin}`,'http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'],
  89. methods:['GET','POST','PUT','DELETE'],
  90. credentials: true // enable set cookie
  91. }));
  92. */
  93. // all of our routes will be prefixed with /api
  94. app.use('/api/pbl', edurouter);
  95. app.use('/api/mysql-pbl', medurouter);
  96. app.use('/api/mongo', mongo);
  97. app.use('/api/weixin', weixin);
  98. app.use('/api/cocoflow', cocoflow);
  99. app.use('/api/szdjg', szdjg);
  100. app.use('/api/bat/getToken', async function (req, res, next) {
  101. let ticket = req.query.ticket
  102. await baoantoken.getToken(ticket, res)
  103. });
  104. // app.use('/game', game);
  105. app.all('/download', function (req, res, next) {
  106. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  107. request({
  108. url: req.body.url,
  109. method: "GET",
  110. encoding: null,
  111. headers: {
  112. 'Accept-Encoding': 'gzip, deflate'
  113. }
  114. },
  115. function (error, response, body) {
  116. if (!error && response.statusCode == 200) {
  117. res.setHeader('Content-Type', 'application/force-download');
  118. res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(req.body.url));
  119. res.setHeader('Content-Length', body.length);
  120. res.send(body);
  121. }
  122. });
  123. });
  124. // 捕获未处理的异常
  125. process.on('uncaughtException', (error) => {
  126. console.error("未处理的异常:", error);
  127. // 这里可以添加更多的错误处理逻辑,比如发送通知或记录日志
  128. });
  129. // 捕获未处理的Promise拒绝
  130. process.on('unhandledRejection', (reason, promise) => {
  131. console.error('未处理的Promise拒绝:', promise, '原因:', reason);
  132. // 这里可以添加更多的错误处理逻辑,比如发送通知或记录日志
  133. });
  134. // START THE SERVER
  135. // =============================================================================
  136. app.listen(port, '0.0.0.0');
  137. console.log('app happens on port ' + port);