🌤 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 --> fileprocess.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("전송 끝")
    }