Prj PJ

[JS] setupProxy.js

솔헬레나 2023. 8. 11. 22:51

어플리케이션에서 /api로 시작되는 endpoint가 서버에 통신을 요청하는 경우 proxy서버가 중계 역할을 하여 target으로 지정한 서버주소로 통신하면서 서버를 우회한다.

 

 

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app, profileType) {

  var targetUrl = "로컬개발서버주소";
  if(profileType == 'dev') {
    targetUrl = "개발서버주소/api";
  }

  console.log('proxy targetUrl', targetUrl);

  app.use(
    createProxyMiddleware('/api', {
      target: targetUrl,
      changeOrigin: true,
      secure: false,
      pathRewrite: {
        '^/api': '' // URL ^/api -> 공백 변경
      }
    })
  )
}

 

https://jihyee.tistory.com/8