본문 바로가기

Nodejs

CORS error Access-Control-Allow-Origin Multiple type, nodejs

반응형

흔히 보는 CORS Access-control-allow-origin error

참고:

 

Single type

res.setHeader("Access-Control-Allow-Origin", "yourdomain" );

 

multiple type

 
app.use((req, res, next) => {
const allowedOrigins = ["http://localhost:3000", "https://yourdomain.co", "https://otheryourdomain.co"];
const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
 

next();
});

 

wildcard type (보안상 추천드리고 싶지않음)

res.header("Access-Control-Allow-Origin","*")

LIST

'Nodejs' 카테고리의 다른 글

mongoDB connect  (0) 2023.08.02