app.js 3.5 KB

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