app.js 3.4 KB

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