๐ŸŒค BE TIL Day 6 0321

โฌ‡๏ธ Main Note
https://docs.google.com/document/d/19xINO8jcj_vMobces2Yf5BpyP-HbKUjjccS7BwBSUU0/edit


โ˜๏ธ Async/Await

Two computers are requesting and responsing to each other to deliver the data.
But sometimes, the request isn't sent properly.
--> This is because the user requested to fetch the information even before the data is fully uploaded to the data base.
To solve this problem, here, we use async/await.

Asynchronization

  • Operated while the other functions are getting executed
  • It's like us using kakao-talk while downloading the game
    --> we don't wait for the game to download successfully.
    Synchronization
  • The functions are executed one by one.
  • After one function is being executed, then the user is now available to send and receive the request successfully.

Javascript is in async way.
--> Source code is executed line by line.
--> After a line of code is executed, the next code is being executed.

// ========== ๋™๊ธฐํ˜•์‹ ==========
const data = axios.get(โ€œhttps://koreanjson.comp/posts/1โ€)
console.log(data)	//promise

// ========== ๋น„๋™๊ธฐ๋ฅผ ๋™๊ธฐ๋กœ ๋ฐ”๊พธ๊ธฐ ==========
async function createBoard(){
	const data = await axios.get(โ€œhttps://koreanjson.comp/posts/1โ€)

โ˜๏ธ Deploy

.env --> file
process.env.๋ณ€์ˆ˜๋ช… --> how to import
Since .env files are for important data, we rarely git push to git hub with the source code.

// ========== API Create ==========
app.post('/tokens/phone', (req, res) => {
  // facade pattern makes the efficiency go higher
  const phone = req.body.phoneNumber

  const isValid = getPhoneNumber(phone)
    if (isValid){
        const myToken = getToken()
        sendTokenToSMS(phone, myToken)
        res.send("์ธ์ฆ์™„๋ฃŒ " + myToken)
    }
})
// ========== API Structure ==========
export async function sendTokenToSMS (fff, ggg){
    // console.log(fff + " ๋ฒˆํ˜ธ๋กœ ์ธ์ฆ๋ฒˆํ˜ธ " + ggg + "๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค")

    const appKey = process.env.SMS_APP_KEY
    const XSecretKey = process.env.SMS.X_SECRET_KEY
    const sender = process.env.SNS_SENDER

    const result = await axios.post( `https://api-sms.cloud.toast.com/sms/v3.0/${appKey}/WNgOBwLIMMiRMbfw/sender/sms`,{  //endpoint
        // data
        body: `ใ…Žใ…‡ใ…Žใ…‡, ์ธ์ฆ๋ฒˆํ˜ธ๋Š” [${ggg}]์ž„`,
        sendNo: sender,  // ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ
        recipientList:[
            // ๋ฐ›๋Š” ์‚ฌ๋žŒ (๋™์‹œ์— ์—ฌ๋Ÿฌ๋ช…์—๊ฒŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ธฐ์— ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์คŒ)
            {internationalRecipientNo: fff}
        ]
    },{
        headers: {
            //config - ์„ค์ •ํŒŒ์ผ
            "Content-Type": "application/json;charset-URF-8",
            "X-Secret-Key": XSecretKey
        }
    })
    console.log(result)
    console.log("์ „์†ก ๋")
}


์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ