app.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 port = "7003"; // set our port
  15. // const cors = require('cors')
  16. app.use(morgan('dev'));
  17. // configure app to use bodyParser()
  18. // this will let us get the data from a POST
  19. app.use(bodyParser.urlencoded({ extended: true, limit: '3mb' }));
  20. app.use(bodyParser.json({ limit: '3mb' }));
  21. // app.use(cors());
  22. //暂时全跨域
  23. app.use(function (req, res, next) {
  24. ///var allowedOrigins = [config.local.origin,'http://cocorobo.hk','http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'];
  25. var origin = req.headers.origin || "*";
  26. //if(allowedOrigins.indexOf(origin) > -1){
  27. res.setHeader('Access-Control-Allow-Origin', origin);
  28. //}
  29. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  30. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  31. res.header('Access-Control-Allow-Credentials', true);
  32. //修改程序信息与版本
  33. res.header('X-Powered-By', ' 3.2.1')
  34. //内容类型:如果是post请求必须指定这个属性
  35. res.header('Content-Type', 'application/json;charset=utf-8')
  36. next();
  37. });
  38. /*
  39. app.use(cors({
  40. origin:[`http:${config.local.origin}`,'http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'],
  41. methods:['GET','POST','PUT','DELETE'],
  42. credentials: true // enable set cookie
  43. }));
  44. */
  45. // all of our routes will be prefixed with /api
  46. app.use('/api/pbl', edurouter);
  47. app.use('/api/mongo', mongo);
  48. app.use('/api/weixin', weixin);
  49. app.use('/api/cocoflow', cocoflow);
  50. app.use('/api/szdjg', szdjg);
  51. app.use('/api/bat/getToken', async function (req, res, next) {
  52. let ticket = req.query.ticket
  53. await baoantoken.getToken(ticket, res)
  54. });
  55. // app.use('/game', game);
  56. app.all('/download', function (req, res, next) {
  57. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  58. request({
  59. url: req.body.url,
  60. method: "GET",
  61. encoding: null,
  62. headers: {
  63. 'Accept-Encoding': 'gzip, deflate'
  64. }
  65. },
  66. function (error, response, body) {
  67. if (!error && response.statusCode == 200) {
  68. res.setHeader('Content-Type', 'application/force-download');
  69. res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(req.body.url));
  70. res.setHeader('Content-Length', body.length);
  71. res.send(body);
  72. }
  73. });
  74. });
  75. // 捕获未处理的异常
  76. process.on('uncaughtException', (error) => {
  77. console.error("未处理的异常:", error);
  78. // 这里可以添加更多的错误处理逻辑,比如发送通知或记录日志
  79. });
  80. // 捕获未处理的Promise拒绝
  81. process.on('unhandledRejection', (reason, promise) => {
  82. console.error('未处理的Promise拒绝:', promise, '原因:', reason);
  83. // 这里可以添加更多的错误处理逻辑,比如发送通知或记录日志
  84. });
  85. // START THE SERVER
  86. // =============================================================================
  87. app.listen(port, '0.0.0.0');
  88. console.log('app happens on port ' + port);