app.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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('./ssti');
  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. //暂时全跨域
  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. res.setHeader('Access-Control-Allow-Origin', "https://cxcy.ssti.net.cn"); //线上
  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((req, res, next) => {
  35. res.Back = (code, msg, data) => res.send({ status: code, msg: msg, data: data });
  36. next();
  37. })
  38. /*
  39. app.use(cors({
  40. origin:[`http:${config.local.origin}`,'http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'],
  41. methods:['GET','POST','PUT','DELETE'],
  42. credentials: true // enable set cookie
  43. }));
  44. */
  45. // all of our routes will be prefixed with /api
  46. app.use('/api', edurouter);
  47. // app.use('/game', game);
  48. app.all('/download', function(req, res, next) {
  49. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  50. request({
  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('Content-Disposition', 'attachment; filename=' + path.basename(req.body.url));
  62. res.setHeader('Content-Length', body.length);
  63. res.send(body);
  64. }
  65. });
  66. });
  67. // START THE SERVER
  68. // =============================================================================
  69. app.listen(port);
  70. console.log('app happens on port ' + port);