app.js 3.6 KB

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