文件服务后台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

66 rindas
1.6 KiB

  1. 'use strict'
  2. /**
  3. * proxy api http request to rongxin.gov.rgvn.api service
  4. */
  5. const proxy = require('http-proxy-middleware')
  6. const config = require('config')
  7. const logFactory = require('../utils/logFactory')
  8. const logUtil = require('../utils/logUtil')
  9. const debug = logFactory(logUtil())('rongxin.gov.rgvn.dashboard.tiandeng:routes:apiProxy')
  10. class ApiProxy {
  11. constructor(policyRouter) {
  12. this._policyRouter = policyRouter
  13. }
  14. onProxyReq (proxyReq, req, res, options) {
  15. const method = 'proxyReq'
  16. debug(method, '[Proxy-Enter]', req.path)
  17. if (req.path.startsWith('/api')) {
  18. proxyReq.path = proxyReq.path.substring(4)
  19. debug(method, 'proxyReq.path: ', proxyReq.path, req.path)
  20. }
  21. }
  22. onProxyRes (proxyRes, req, res) {
  23. let method = "onProxyRes"
  24. debug('[Proxy-Exit]', req.path)
  25. debug('[onProxyRes] ', 'req.path = ', req.path, 'req.method = ', req.method)
  26. }
  27. onError (err, req, res) {
  28. let method = 'onError'
  29. debug.error(method, err)
  30. }
  31. init () {
  32. const self = this
  33. const govRgvnApiServiceUrl = `${config.get('rongxin_gov_rgvn_api_service').host}:${config.get('rongxin_gov_rgvn_api_service').port}`;
  34. const proxyInst = proxy({
  35. target: govRgvnApiServiceUrl,
  36. router: {
  37. },
  38. changeOrigin: true,
  39. logLevel: 'debug',
  40. logProvider: () => debug,
  41. onProxyRes: self.onProxyRes.bind(self),
  42. onProxyReq: self.onProxyReq.bind(self),
  43. onError: self.onError.bind(self)
  44. })
  45. self._policyRouter.use('/api', proxyInst, {
  46. name: 'apiProxy.proxyInst'
  47. });
  48. debug.info(`api proxy, api service uri: ${govRgvnApiServiceUrl}`)
  49. return self
  50. }
  51. }
  52. module.exports = ApiProxy