Tooravel - ๐Ÿ“ฑ๐Ÿ“ฒ req, res url ์ˆ˜์ •

Doodreamยท2021๋…„ 4์›” 4์ผ
1

Tooravel ํ”„๋กœ์ ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

๐Ÿ“ฑ๐Ÿ“ฒ request, response Url ์ˆ˜์ •

์ „ ์‹œ๋ฆฌ์ฆˆ์—์„œ ๋ฐฑ์—”๋“œ ๋ฐฐํฌ๊นŒ์ง€ ์™„์„ฑํ•˜์˜€๋‹ค๋ฉด ์ด์ œ ๋ฐฐํฌ๋œ ๋„๋ฉ”์ธ์„ ๊ธฐ์ค€์œผ๋กœ ํ™•์ •๋œ ํ”„๋ก ํŠธ ์•ค๋“œ์™€ ๋ฐฑ์—”๋“œ ์‚ฌ์ด์˜ url์„ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ“ฑ FrontEnd Fetch Url์ˆ˜์ •

    makeUrl: (url) => {
        let obj = {};
        if (url[0] === '/') {
            obj['external'] = false;

            if (process.env.NODE_ENV === 'development') {
                obj['fullUrl'] = 'http://localhost:8080' + url;
            }
            else if (process.env.NODE_ENV === 'production') {
                obj['fullUrl'] = 'https://tooravel.be' + url;
            }
            else {
                new Error('process.env.NODE_ENV is not on development or production')
            }
   
          

ํ”„๋ก ํŠธ ๋‹จ์—์„œ๋Š” fetch ํ•จ์ˆ˜๋ฅผ ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐœํŽธํ•ด์„œ Fetch ์œ ํ‹ธ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉ์ค‘์ด๋‹ค.

Feteh = {
  makeUrl: () => { ...
  }
  get:() => { 
    ...
     return fetch(fullUrl, ...
  }
  post: () => {
    ...
    return fetch(fullUrl, ...
  }
}
  

์ด๋Ÿฌํ•œ ํ˜•ํƒœ์™€์ค‘์— ์ƒ๋‹จ์˜ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด ์žˆ๋Š”์…ˆ์ด๋‹ค. ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ url์„ ๋ฐ”๊ฟ”์ฃผ๋Š”๋ฐ, ๋นŒ๋“œ๋ฅผ ํ•˜์˜€๋‹ค๋ฉด process.env.NODE_ENV ๊ฐ€ production ์•„๋‹ˆ๋ฉด development๊ฐ€ ๋˜๋Š” ๊ฒƒ์„ ์ด์šฉํ•œ ๊ฒƒ์ด๋‹ค.

makeUrl ๋ถ€๋ถ„์˜ https://tooravel.be ๋กœ ์ˆ˜์ •ํ•ด๋‘๋ฉด ์ดํ›„ ์š”์ฒญํ•˜๋Š” url๋“ค์ด ๋’ค์— ๋ถ™์–ด์„œ ์š”์ฒญ๋œ๋‹ค.

๐Ÿ“ฒ Backend cors ์—๋Ÿฌ

โ“ CORS ์ด์Šˆ๋ž€?

๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ… SOP (Same-Origin Policy)

ํ”„๋กœํ† ์ฝœ/ํ˜ธ์ŠคํŠธ/ํฌํŠธ ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ๊ฐ™์€ ์ถœ์ฒ˜์˜ ์š”์ฒญ์ด๋ผ ๋ฐ›์•„๋“ค์ด์ง€ ์•Š๊ณ  ๊ฑฐ๋ถ€ํ•˜๋Š” ์ •์ฑ…์œผ๋กœ์„œ ์œ„ ์…‹์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋‹ค๋ฅด๋‹ค๋ฉด ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ์š”์ฒญ์œผ๋กœ ๋ฐ›์•„๋“ค์—ฌ ๊ฑฐ๋ถ€ํ•˜๋Š” ์ •์ฑ…์ด๋‹ค. ์ด ์ •์ฑ…์€ ๋‹ค๋ฅธ ์ถœ์ฒ˜์—์„œ ๊ฐ€์ ธ์˜จ ์Šคํฌ๋ฆฝํŠธ์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š”๊ฒƒ์„ ์ œํ•œํ•˜๋Š” ๋งค์šฐ ๋ณด์•ˆ์— ์ค‘์š”ํ•œ ์ •์ฑ…์ด๋‹ค.

์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” (๋‹ค๋ฅธ ์ถœ์ฒ˜๋กœ ๋ถ€ํ„ฐ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ๋Š”) ๋ฐฉ๋ฒ•์œผ๋กœ์„œ CORS(Cross-Origin Resource Sharing) ์ •์ฑ…๊ณผ ๊ด€๋ จํ•˜์—ฌ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

CORS (Cross-Origin Resource Sharing)

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— http ํ†ต์‹ ์„ ํ• ๋•Œ ์ƒ๋Œ€๋ฐฉ ์ธก์˜ ์ถœ์ฒ˜๋ฅผ ๋ฏธ๋ฆฌ ์ธ์ง€ํ•˜๊ณ  ํ•ด๋‹น ์ถœ์ฒ˜์— ๋Œ€ํ•œ ์ •๋ณด๋“ค์— ๋Œ€ํ•ด์„œ ์„œ๋กœ ์ƒํ˜ธ์ž‘์šฉ ํ• ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์ด๋ฐฉ๋ฒ•์€ ํ†ต์‹ ์ •๋ณด์˜ ํ—ค๋”์— ์„œ๋กœ์˜ ๋„๋ฉ”์ธ์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•จ์œผ๋กœ์„œ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฃผ๋กœ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์˜ ํ—ค๋”์— ๋„๋ฉ”์ธ์ •๋ณด๋ฅผ ๊ธฐ์ž…ํ•œ๋‹ค. ์ฆ‰, ๋ฐฑ์—”๋“œ์—์„œ cors ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•œ๋‹ค.
Access-Control-Allow-Origin: http://example.com

ํ”„๋ก์‹œ ์„œ๋ฒ„

ํ”„๋ก์‹œ ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์‚ฌ์ด์—์„œ ์ •๋ณด๊ตํ™˜์„ ๋„์™€์ฃผ๋Š” ์„œ๋ฒ„์ด๋‹ค. ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๊ณ ์ž ํ•˜๋Š” ์„œ๋ฒ„์˜ Access-Control-Allow-Origin ์†์„ฑ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์— ๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๋‹ค. ํ”„๋ก์‹œ ์„œ๋ฒ„๊ฐ€ ์‹ค์ œ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ด์„œ ๋ฐ›์•„์˜จ ๋‹ค์Œ ๊ทธ๊ฑธ Access-Control-Allow-Origin ์„ค์ •์„ ์ ์ ˆํžˆ ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋Œ๋ ค์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.


์—ฌ๊ธฐ์„œ express js์˜ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ cors ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

const cors = require('cors');

app.use(cors({
    origin: ['https://tooravel-front.vercel.app'];
    credentials: true,
    optionsSuccessStatus: 200
}));

cors ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์œ„์™€ ๊ฐ™์ด origin์— ํ—ˆ์šฉํ•˜๋Š” ํ”„๋ก ํŠธ์•ค๋“œ์˜ ๋„๋ฉ”์ธ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅ ํ•˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ํ”„๋กœํ† ์ฝœ/ ํ˜ธ์ŠคํŠธ/ํฌํŠธ ์— ํ•ด๋‹นํ•˜๋Š” ์š”์ฒญ์— ๋Œ€ํ•ด์„œ ์ƒํ˜ธ์ž‘์šฉ์„ ํ• ์ˆ˜ ์žˆ๋‹ค.

์œ„ ์™€ ๊ฐ™์ด ๋ฐฑ์—”๋“œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด

๋“œ๋””์–ด ๋‚ด๊ฐ€ ๋งŒ๋“ ํŽ˜์ด์ง€์—์„œ ๋กœ๊ทธ์ธ์ด ๋œ๊ฒƒ ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.!

profile
์ผ์ƒ์„ ๊ธฐ๋กํ•˜๋Š” ์‚ถ์„ ์‚ฌ๋Š” ๊ฐœ๋ฐœ์ž โœ’๏ธ #front_end ๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€