🌀 BE TIL Day 6 0321

JBΒ·2022λ…„ 3μ›” 21일
0

CodeCamp BE 02

λͺ©λ‘ 보기
6/30

⬇️ 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("전솑 끝")
}


profile
두비두λ°₯λ°₯

0개의 λŒ“κΈ€