app.js 3.2 KB

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