티스토리 뷰

const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use( proxy('/posts', {
target:'https://target.domain.com',
changeOrigin: true
})
);
};

흔히 cors 문제를 해결하기 위해 프록시를 사용할 수 있는데, http-proxy-middleware를 사용해서 위처럼 설정하면 /posts로 시작하는 API는 target 필드에 설정한 서버로 호출하게 된다. 즉 /posts/1 을 호출하면, https://target.domain.com/posts/1 을 호출하게 된다.

const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use( proxy('/old-posts', {
pathRewrite: {
'^/old-posts': '/new-posts'
},
target:'https://target.domain.com',
changeOrigin: true
})
);
};

pathRewrite 객체는 key에 매핑된 api path를 value로 치환해준다. 위의 경우에는 /old-posts를 호출하면 https://target.domain.com/new-posts 를 호출하게 된다.

Ref

https://github.com/chimurai/http-proxy-middleware#tldr-

'공부일지(TIL) > Web' 카테고리의 다른 글

[Web] JWT  (0) 2022.08.10
[SEO] Canonical tag  (0) 2022.08.09
자바스크립트로 파일 다운로드하기  (0) 2022.03.04
Static Rendering vs Server Side Rendering  (0) 2021.11.15
package.json의 resolutions  (0) 2021.10.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함