728x90
cors에러 경우
CORS(Cross-Origin Resource Sharing)
자기가 속하지 않은 포트로 정보 보내려고 할 때 주소가 달라서 보안상 안된다고 하는것
얘는 별도처리 없이
모두에게 허용하는 방법임!!
const cors = require("cors");
하고
구동되는 함수에
app.use(cors());
이렇게 하면 해결된다.
특정 도메인에만 허용하고 싶으면 변수에 특정 도메인 주소만 넣으면 됨.
let corsOptions = {
origin: 'https://www.domain.com',
credentials: true
}
app.use(cors(corsOptions));
특정 도메인을 허용할 때 사용!
Access-Control-Allow-Origin response 헤더를 추가하는 방법도 있음.
app.get('/', (req,res) => {
res.header("Access-Control-Allow-Origin", "*");
...
}
미들웨어 서버 이용하기
proxy
proxy를 쓰는 경우 밑에 유튜브 영상처럼
/api 설정을 해줘야함.!
client에
setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:3001',
changeOrigin: true
})
);
};
출처:
https://surprisecomputer.tistory.com/32
[Node.js] express cors 사용하기
1. 서론 리버스 프록시 서버로 NGINX를 두고 한 워크스테이션에서 Swagger와 node.js 서버를 함께 구동한 적이 있다. Swagger의 포트를 8085로 지정하고 node.js 서버는 443번으로 지정했는 데, CORS 에러가 발
surprisecomputer.tistory.com
https://firework-ham.tistory.com/70
https://www.youtube.com/watch?v=BHSJw8PDwj0&list=PL9a7QRYt5fqkZC9jc7jntD1WuAogjo_9T&index=22
728x90
'블록체인' 카테고리의 다른 글
에러원인 및 해결 expected property shorthand object shorthand / app.post(`보낼주소`),보낼정보 | app.get(`보낼주소`).다시요청받은 정보 어떻게 쓸지 (0) | 2022.02.08 |
---|---|
map함수로 배열값 데이터 보여주기 (에러해결 uncaught typeerror cannot read properties of undefined) (0) | 2022.02.08 |
220208 수업 스마트컨트랙트 (0) | 2022.02.08 |
문자열 숫자로 반환하기 parseInt(string) (0) | 2022.02.08 |
블록체인 naive코드풀이 #4 지갑 (지갑생성, 원하는 경로, 잔고, 코인전송) (0) | 2022.02.03 |