server.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const superagent = require('superagent')
  2. const express = require('express')
  3. const path = require('path')
  4. const app = express()
  5. const bodyParser = require('body-parser')
  6. app.use(bodyParser.urlencoded({ extended: true }));
  7. app.use(bodyParser.json())
  8. let mycookie = '' // 自行添加 Cookie
  9. app.all('*', function(req, res, next) {
  10. res.header("Access-Control-Allow-Origin", "*")
  11. res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
  12. res.header("Access-Control-Allow-Headers", "X-Requested-With")
  13. res.header('Access-Control-Allow-Headers', 'Content-Type')
  14. next()
  15. })
  16. app.get('/*', (req, res) => {
  17. let ServerCookie = req.headers.cookie + ";" + mycookie
  18. superagent
  19. .get(`https://dev.test.gitinn.com${req.url}`)
  20. .send(req.query)
  21. .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
  22. .set('Cookie', ServerCookie)
  23. .end((err, resRequest) => {
  24. if (err) console.log(err)
  25. else res.end(JSON.stringify(resRequest.body))
  26. })
  27. })
  28. app.post('/*', (req, res) => {
  29. let ServerCookie = req.headers.cookie + ";" + mycookie
  30. // console.log(req.body)
  31. superagent
  32. .post(`https://dev.test.gitinn.com${req.url}`)
  33. .send(req.body)
  34. .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
  35. .set('Cookie', ServerCookie)
  36. .end((err, resRequest) => {
  37. // console.log(mycookie)
  38. // 设置 Cookie
  39. if(!mycookie) mycookie = resRequest.headers['set-cookie']
  40. if (err) console.log(err)
  41. else res.end(JSON.stringify(resRequest.body))
  42. })
  43. })
  44. app.listen(8082, "localhost", () => {
  45. console.log("run in http://localhost:8082")
  46. })