app.js 3.2 KB

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