app.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = "7333"; // 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. //暂时全跨域
  17. app.use(function (req, res, next) {
  18. ///var allowedOrigins = [config.local.origin,'http://cocorobo.hk','http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'];
  19. var origin = req.headers.origin || "*";
  20. //if(allowedOrigins.indexOf(origin) > -1){
  21. res.setHeader("Access-Control-Allow-Origin", origin);
  22. //}
  23. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  24. res.header(
  25. "Access-Control-Allow-Headers",
  26. "Origin, X-Requested-With, Content-Type, Accept"
  27. );
  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. let verifToken = require("./token.js");
  43. app.use(verifToken);
  44. // all of our routes will be prefixed with /api
  45. app.use("/api/pbl", edurouter);
  46. // app.use('/game', game);
  47. app.all("/download", function (req, res, next) {
  48. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  49. request(
  50. {
  51. url: req.body.url,
  52. method: "GET",
  53. encoding: null,
  54. headers: {
  55. "Accept-Encoding": "gzip, deflate",
  56. },
  57. },
  58. function (error, response, body) {
  59. if (!error && response.statusCode == 200) {
  60. res.setHeader("Content-Type", "application/force-download");
  61. res.setHeader(
  62. "Content-Disposition",
  63. "attachment; filename=" + path.basename(req.body.url)
  64. );
  65. res.setHeader("Content-Length", body.length);
  66. res.send(body);
  67. }
  68. }
  69. );
  70. });
  71. // START THE SERVER
  72. // =============================================================================
  73. app.listen(port);
  74. console.log("app happens on port " + port);