app.js 5.2 KB

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