app.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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('./edurouter');
  7. const morgan = require('morgan');
  8. var path = require("path");
  9. var port = "7004"; // 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("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  25. res.header('Access-Control-Allow-Credentials', true);
  26. //修改程序信息与版本
  27. res.header('X-Powered-By', ' 3.2.1')
  28. //内容类型:如果是post请求必须指定这个属性
  29. res.header('Content-Type', 'application/json;charset=utf-8')
  30. next();
  31. });
  32. /*
  33. app.use(cors({
  34. origin:[`http:${config.local.origin}`,'http://www.cocorobo.hk','https://cocorobo.hk','http://cloud.cocorobo.hk','https://cloud.cocorobo.hk'],
  35. methods:['GET','POST','PUT','DELETE'],
  36. credentials: true // enable set cookie
  37. }));
  38. */
  39. // all of our routes will be prefixed with /api
  40. app.use('/edu', edurouter);
  41. // app.use('/game', game);
  42. app.all('/download', function(req, res, next) {
  43. //req.body.url = "https://ccrb.s3.cn-northwest-1.amazonaws.com.cn/%E4%B8%8B%E8%BD%BD%20%284%29.doc";
  44. request({
  45. url: req.body.url,
  46. method: "GET",
  47. encoding: null,
  48. headers: {
  49. 'Accept-Encoding': 'gzip, deflate'
  50. }
  51. },
  52. function(error, response, body) {
  53. if (!error && response.statusCode == 200) {
  54. res.setHeader('Content-Type', 'application/force-download');
  55. res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(req.body.url));
  56. res.setHeader('Content-Length', body.length);
  57. res.send(body);
  58. }
  59. });
  60. });
  61. // START THE SERVER
  62. // =============================================================================
  63. app.listen(port);
  64. console.log('app happens on port ' + port);