app.js 5.3 KB

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