app.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const express = require('express');
  2. const http = require('http');
  3. const router = express.Router();
  4. const app = express();
  5. const fs = require('fs');
  6. var bodyParser = require("body-parser");
  7. var request = require("request");
  8. const edurouter = require("./pbl");
  9. const certificate = require("./certificate");
  10. var path = require("path");
  11. var port = "7333"; // set our port
  12. process.env.NODE_ENV = 'dev';
  13. // main.use((req, res, next) => {
  14. // var deviceAgent = req.headers['user-agent'].toLowerCase();
  15. // var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
  16. // if (agentID)
  17. // req.ismobile = true;
  18. // else
  19. // req.ismobile = false;
  20. // next();
  21. // });
  22. app.use(bodyParser.urlencoded({ extended: true, limit: "3mb" }));
  23. app.use(bodyParser.json({ limit: "3mb" }));
  24. //暂时全跨域
  25. app.use(function (req, res, next) {
  26. ///var allowedOrigins = [config.local.origin,'http://cocorobo.hk','http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'];
  27. var origin = req.headers.origin || "*";
  28. //if(allowedOrigins.indexOf(origin) > -1){
  29. res.setHeader("Access-Control-Allow-Origin", origin);
  30. //}
  31. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  32. res.header(
  33. "Access-Control-Allow-Headers",
  34. "Origin, X-Requested-With, Content-Type, Accept"
  35. );
  36. res.header("Access-Control-Allow-Credentials", true);
  37. //修改程序信息与版本
  38. res.header("X-Powered-By", " 3.2.1");
  39. //内容类型:如果是post请求必须指定这个属性
  40. res.header("Content-Type", "application/json;charset=utf-8");
  41. next();
  42. });
  43. /*
  44. app.use(cors({
  45. origin:[`http:${config.local.origin}`,'http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'],
  46. methods:['GET','POST','PUT','DELETE'],
  47. credentials: true // enable set cookie
  48. }));
  49. */
  50. let verifToken = require("./token.js");
  51. app.use(verifToken);
  52. // all of our routes will be prefixed with /api
  53. app.use("/api/pbl", edurouter);
  54. app.use("/api/pbl", certificate);
  55. // 设置静态目录,带虚拟前缀
  56. app.use('/static', express.static(path.join(__dirname, 'static')));
  57. // app.use('/game', game);
  58. app.all("/download", function (req, res, next) {
  59. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  60. request(
  61. {
  62. url: req.body.url,
  63. method: "GET",
  64. encoding: null,
  65. headers: {
  66. "Accept-Encoding": "gzip, deflate",
  67. },
  68. },
  69. function (error, response, body) {
  70. if (!error && response.statusCode == 200) {
  71. res.setHeader("Content-Type", "application/force-download");
  72. res.setHeader(
  73. "Content-Disposition",
  74. "attachment; filename=" + path.basename(req.body.url)
  75. );
  76. res.setHeader("Content-Length", body.length);
  77. res.send(body);
  78. }
  79. }
  80. );
  81. });
  82. // START THE SERVER
  83. // =============================================================================
  84. app.listen(port);
  85. console.log("app happens on port " + port);
  86. app.use(function (req, res, next) {
  87. res.send("404 Not Found");
  88. });
  89. module.exports = app;