2023.03.20 TIL

์ •์Šน์›ยท2023๋…„ 3์›” 22์ผ
0
post-thumbnail

๐Ÿ“’ ๋ชฉ์ฐจ

  • ๐Ÿ“Œ Axios
  • ๐Ÿ“Œ Axios ์„ค์น˜ํ•˜๊ธฐ
  • ๐Ÿ“Œ Axios ์‘๋‹ต ์ œ์–ดํ•˜๊ธฐ
  • ๐Ÿ“Œ Axios HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ ์ข…๋ฅ˜
  • ๐Ÿ“Œ Callback
  • ๐Ÿ“Œ ๋น„๋™๊ธฐ์  Callback
  • ๐Ÿ“Œ Promise
  • ๐Ÿ“Œ async/await
  • ๐Ÿ“Œ Axios๋กœ GET ์š”์ฒญ ๋ณด๋‚ด๊ธฐ (๋™๊ธฐ, ๋น„๋™๊ธฐ ๋ฐฉ์‹)
  • ๐Ÿ“Œ SMS ์ „์†ก
  • ๐Ÿ“Œ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋ถ„๋ฆฌ
  • ๐Ÿ“Œ EMAIL ์ „์†ก

๐Ÿ“Œ Axios

Axios๋Š” ๋ธŒ๋ผ์šฐ์ €์™€ Node.js๋ฅผ ์œ„ํ•œ HTTP ๋น„๋™๊ธฐ ํ†ต์‹  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
ํŠน์ง•์œผ๋กœ๋Š” Promise api๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  async/await api๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
๋˜ํ•œ, HTTP ์š”์ฒญ๊ณผ ์‘๋‹ต์„ JSON ํ˜•ํƒœ๋กœ ์ž๋™์œผ๋กœ ๋ณ€๊ฒฝ์‹œ์ผœ์ค€๋‹ค.

์˜ค๋Š˜์€ axios๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์— request๋ฅผ ๋ณด๋‚ด๊ณ  response๋ฅผ ๋ฐ›์•„ ๊ฒฐ๊ณผ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด ํ•™์Šตํ•˜์˜€๋‹ค.

๐Ÿ“Œ Axios ์„ค์น˜ํ•˜๊ธฐ

axios๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ, yarn์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

yarn add axios

๐Ÿ“Œ Axios ์‘๋‹ต ์ œ์–ดํ•˜๊ธฐ

โœ… .then

axios ํ†ต์‹ ์—์„œ ๋น„๋™๊ธฐ ํ†ต์‹ ์ด ์„ฑ๊ณตํ–ˆ์„ ๊ฒฝ์šฐ, .then()์€ ์ฝœ๋ฐฑ์„ ์ธ์ž๋กœ ๋ฐ›์•„ ๊ฒฐ๊ณผ๊ฐ’์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ… .catch

.catch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. error๊ฐ์ฒด์—์„œ ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด .catch์—์„œ ๋ฐ›์•„์˜ค๋Š” error ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์‘๋‹ต์ƒํƒœ์ฝ”๋“œ์™€ ์‘๋‹ตํ—ค๋” ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

axios.get('/hello')
		 .catch(function (error) {
				if (error.response) {
					console.log(error.response.status);
					console.log(error.response.headers);
				}
			}

๐Ÿ“Œ Axios HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ ์ข…๋ฅ˜

axios์—๋Š” ์—ฌ๋Ÿฌ ์š”์ฒญ ๋ฉ”์†Œ๋“œ๋“ค์ด ์กด์žฌํ•œ๋‹ค. ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋Š” axios.get(),
์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋Š” axios.post(), ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” axios.put ๊ทธ๋ฆฌ๊ณ  ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” axios.delete๊ฐ€ ์žˆ๋‹ค.

โœ… axios.get(url, [config])

โœ… axios.post(url, data[config])

โœ… axios.put(url, data[config])

โœ… axios.delete(url, [config])

๐Ÿ“Œ Callback

callback์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฐœ๋…์ธ๋ฐ, ์˜๋ฏธ๋กœ๋Š” ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
๋จผ์ €, ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ด์šฉ๋˜๋Š” ํ•จ์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ์ด๋ฒคํŠธ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด์ง€๋Š” ํ•จ์ˆ˜์ด๋‹ค.

โœ… ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ด์šฉ๋˜๋Š” ํ•จ์ˆ˜

ํ•จ์ˆ˜์—๋Š” ๋‹จ์ˆœํžˆ ๊ฐ’๋งŒ ์ธ์ž๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ์ธ์ž๋กœ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด func ํ•จ์ˆ˜์— ์ธ์ž๋กœ callbackํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€ ํ•จ์ˆ˜ ์•ˆ์—์„œ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

function func (x, y, callback){
	let result = x * y
	callback(result)
}
>
function result (data) {
	console.log(data, "์ฝœ๋ฐฑํ•จ์ˆ˜ ์‹คํ–‰")
}
>
add(5, 10, result)

โœ… ์ด๋ฒคํŠธ์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด์ง€๋Š” ํ•จ์ˆ˜

์ง€๊ธˆ๊นŒ์ง€ ํ•™์Šตํ•ด์™”๋˜ ์ด๋ฒคํŠธ๋Š” onclick๊ณผ onchange๊ฐ€ ์žˆ๋‹ค.
์ด ๋‘ ์ด๋ฒคํŠธ๋Š” HTML์—์„œ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“์€ ํ•จ์ˆ˜์ด๋ฉฐ, ์˜ˆ๋ฅผ๋“ค์–ด ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๊ฒŒ ๋˜๋ฉด, onclick์˜ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๊ทธํ›„ onclick์—์„œ ํ•ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
์ฆ‰, ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์—ฌ๊ธฐ์„œ callbackํ•จ์ˆ˜๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ callback ํ•จ์ˆ˜๋Š” 2๊ฐ€์ง€๋กœ ๋‹ค์‹œ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋™๊ธฐ์  ํ•จ์ˆ˜์™€ ๋น„๋™๊ธฐ์  ํ•จ์ˆ˜์ด๋‹ค.
๋™๊ธฐ์  ๋ฐฉ์‹์ด๋ž€, ํ˜„์žฌ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋Š” ์ž‘์—…์ด ๋๋‚œ ๋’ค์—, ๋‹ค์Œ ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ๋น„๋™๊ธฐ์  ๋ฐฉ์‹์ด๋ž€ ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์ž‘์—…์˜ ์™„๋ฃŒ ์—ฌ๋ถ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ฆ‰์‹œ ๋‹ค๋ฅธ ์ฝ”๋“œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” single-thread / non-blocking ๋ฐฉ์‹์˜์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•ด์„œ js๋Š” ํ•œ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ฝ”๋“œ๋งŒ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์‹คํ–‰์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ“Œ ๋น„๋™๊ธฐ์  Callback

๋น„๋™๊ธฐ์  callbackํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ๊ฐ€์žฅ ์ข‹์€ ์˜ˆ์‹œ๋Š” setTimeout์ด๋‹ค.
setTimeout์€ ์ผ์ • ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜๊ณ , ๊ทธ ์‹œ๊ฐ„ ์ดํ›„์— ์ž‘์—…์ด ์‹œ์ž‘ ๋˜๋Š” ๊ฒƒ์ธ๋ฐ, ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘์„ ์ง„ํ–‰ํ•˜๋ฉด
"์ด๊ฑด ๋ฐ”๋กœ ์‹คํ–‰"์ด ๋จผ์ € ๋‚˜ํƒ€๋‚˜๊ณ  "3์ดˆ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ"๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.
"3์ดˆ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ"๋ฅผ ๋จผ์ € ์‹คํ–‰ํ–ˆ๋Š”๋ฐ ์ด์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์ด์œ ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

function Test () {
	console.log("3์ดˆ ๊ธฐ๋‹ค๋ฆฌ๊ธฐ")
}
>
setTimeout(Test,3000);
console.log('์ด๊ฑด ๋ฐ”๋กœ ์‹คํ–‰')

๐Ÿ“Œ Promise

์•ž์„œ ํ•™์Šตํ•œ ๋น„๋™๊ธฐ์  callback์„ ์—ฐ์†ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด callback ์ง€์˜ฅ์— ๋น ์ง€๊ฒŒ ๋œ๋‹ค.
callback ์ง€์˜ฅ์ด๋ž€ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์ด ๋ฐ˜๋ณต๋˜์–ด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ ธ ์ฝ”๋“œ๋ฅผ ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์ด์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—๋Š” Promise ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋‹ค.
์ด์ œ promise์™€ axios๋ฅผ ์ด์šฉํ•ด GET ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  data๋ฅผ ๋ฐ›์•„ ๋ณด๋Š” ์‹ค์Šต์„ ํ•ด๋ณด๊ฒ ๋‹ค.

const url = 'https://koreanjson.com/posts/1';
>
function getData() {
  return new Promise(function (resolve, reject) {
    axios.get(url).then(function (response) {
      if (response) {
        resolve(response.data);
      }
      reject(new Error('Request is failed'));
    });
  });
}
>
let result = [];
getData()
  .then(function (data) {
    for (let v of Object.values(data)) {
      result.push(v);
    }
    console.log(result);
  })
  .catch(function (err) {
    console.error(err);
  });

์œ„ ์ฝ”๋“œ์—์„œ resolve ๋˜๋Š” rejectํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ๋ฌธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ, ๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” then์ด๋‚˜ catch๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.
๋”ฐ๋ผ์„œ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ, resolve ๋˜๋Š” reject๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋น„๋™๊ธฐ ์ž‘์—…์˜ ๋™๊ธฐ์  ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ, resolve๋Š” ์„ฑ๊ณตํ–ˆ์„ ๋•Œ์˜ ๋ฐ˜ํ™˜๊ฐ’์ด๊ณ , reject๋Š” ์‹คํŒจํ–ˆ์„ ๋•Œ์˜ ๋ฐ˜ํ™˜๊ฐ’์ด๋‹ค.

๐Ÿ“Œ async/await

axios๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ async์™€ await๋ฅผ ์ง€์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
async์™€ await๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ธฐ์— ์•ž์„œ axios๋ฅผ ์ด์šฉํ•ด GET ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  data๋ฅผ ๋ฐ›์•„์˜ค๋„๋ก ํ•ด๋ณด๊ฒ ๋‹ค.

async function getData(url){
  let data = (await axios.get(url)).data;
	return data;
  }
>
let url = 'https://koreanjson.com/posts/1';
>
async function dataArr(){
    let data = await getData(url);
    let result = [];
    for(let v of Object.values(data)){
        result.push(v);
    }
    console.log(result);
}
>
dataArr();

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒŒ ๋˜๋ฉด ๋น„๋™๊ธฐ ์ž‘์—…์„ ํ•˜๊ณ ์žํ•˜๋Š” ํ•จ์ˆ˜์•ž์— async๋ฅผ ํ‘œ๊ธฐํ•˜๊ณ , ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‹ค์งˆ์ ์ธ ๋น„๋™๊ธฐ ์ž‘์—…์ด ํ•„์š”ํ•œ ์œ„์น˜๋งˆ๋‹ค await๋ฅผ ํ‘œ๊ธฐํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ฆ‰, awat๋ฅผ ํ‘œ๊ธฐํ•œ ๊ฒƒ๋งŒ์œผ๋กœ ๋’ท ๋‚ด์šฉ์„ promise๋กœ ์ž๋™์œผ๋กœ ์ „ํ™˜ํ•˜๊ณ  ํ•ด๋‹น ๋‚ด์šฉ์ด resolve๋œ ์ดํ›„์— ๋‹ค์Œ์œผ๋กœ ์ง„ํ–‰์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ, await์˜ ์œ ๋ฌด๋กœ ๋™๊ธฐ์  ํ•จ์ˆ˜์™€ ๋น„๋™๊ธฐ์ ํ•จ์ˆ˜๋กœ ๊ตฌ๋ถ„์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•ž์„œ ์„ค๋ช…ํ•œ ์ฝœ๋ฐฑ์ง€์˜ฅ ๋ฌธ์ œ๋ฅผ promise์™€ async/await๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ Axios๋กœ GET ์š”์ฒญ ๋ณด๋‚ด๊ธฐ (๋™๊ธฐ, ๋น„๋™๊ธฐ ๋ฐฉ์‹)

yarn add axios๋ฅผ ํ†ตํ•ด axios๋ฅผ ์„ค์น˜ํ•œ ๋’ค์—, ์ด์ œ axios๋ฅผ importํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

import axios from 'axios';
>
// 1. ๋น„๋™๊ธฐ๋ฐฉ์‹
function fetchAsync() {
> 
}
>
fetchAsync();
>
// 2. ๋™๊ธฐ๋ฐฉ์‹
async function fetchSync() {
> 
}
>
fetchSync();

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒŒ ๋˜๋ฉด ๋™๊ธฐ์™€ ๋น„๋™๊ธฐ ๋ฐฉ์‹์˜ ์ „์ฒด์ ์ธ ๊ตฌ์กฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

import axios from 'axios';
>
// 1. ๋น„๋™๊ธฐ๋ฐฉ์‹
function fetchAsync() {
  const result = axios.get("https://koreanjson.com/posts/1");
  console.log("๋น„๋™๊ธฐ๋ฐฉ์‹: ", result); // Promise { <pending> }
}
>
fetchAsync();
>
// 2. ๋™๊ธฐ๋ฐฉ์‹
// async function fetchSync() {
//   //  =>   ํ•จ์ˆ˜ ์ค‘๋ณต ์„ ์–ธ ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜์ž!!! (ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝ)
//   const result = await axios.get("https://koreanjson.com/posts/1");
//   console.log("๋™๊ธฐ๋ฐฉ์‹: ", result.data); // ์ œ๋Œ€๋กœ๋œ ๊ฒฐ๊ณผ => { title: "....." }
// }
const fetchSync = async () => {
  const result = await axios.get("https://koreanjson.com/posts/1");
  console.log("๋™๊ธฐ๋ฐฉ์‹: ", result); // ์ œ๋Œ€๋กœ๋œ ๊ฒฐ๊ณผ => { title: "....." }
  console.log("๋™๊ธฐ๋ฐฉ์‹: ", result.data.title);
};
>
fetchSync();

๐Ÿ“Œ SMS ์ „์†ก

  • Coolsms์„ ์‚ฌ์šฉํ•˜์—ฌ SMS๋ฅผ ์ „์†กํ•˜๋Š” api๋ฅผ ๋งŒ๋“ค๊ณ  ์ „์†ก๊นŒ์ง€ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

Coolsms ํ™ˆํŽ˜์ด์ง€ ๋งํฌ
https://console.coolsms.co.kr/sdk

โœ… API ์ž‘์„ฑํ•˜๊ธฐ

  1. coolsms-node-sdk๋ฅผ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์„ค์น˜ํ•œ๋‹ค.

$ yarn add coolsms-node-sdk

  1. ๋ฒˆํ˜ธ๊ฐ€ ์ „์†ก๋˜๋ฉด ํ† ๊ทผ๋ฒˆํ˜ธ์™€ ๋ฌธ์ž๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ js์—์„œ coolsms-node-sdk๋ฅผ import ํ•œ๋‹ค.
// phone.js
>
import coolsms from 'coolsms-node-sdk';
>
// ...์ƒ๋žต

๐Ÿ“Œ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋ถ„๋ฆฌ

.envํŒŒ์ผ ์ƒ์„ฑํ•˜๊ณ , ๊ทธ ์•ˆ์— ์ค‘์š”ํ•œ ํ‚ค๊ฐ’๋“ค์„ ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค๊ณ  jsํŒŒ์ผ์— ๋ณ€์ˆ˜๋ช…์œผ๋กœ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•œ๋‹ค.

// ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค. ๋ณธ์ธ์˜ ํ‚ค๊ฐ’์„ ๋„ฃ์–ด ์ž‘์„ฑํ•˜์„ธ์š”.
SMS_KEY = NCSIYWB4QAC1NTSP
SMS_SECRET = IECMDSCNKBKFX4MRQD22PRVITDY3ND7K
SMS_SENDER = 01012345678

.env ํŒŒ์ผ์ด ํ•จ์ˆ˜์—์„œ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

import coolsms from 'coolsms-node-sdk';
const mysms = coolsms.default;
>
export async function sendTokenToSMS(myphone, token) {
  const SMS_KEY = process.env.SMS_KEY;
  const SMS_SECRET = process.env.SMS_SECRET;
  const SMS_SENDER = process.env.SMS_SENDER;
>
  const messageService = new mysms(SMS_KEY, SMS_SECRET);
  const result = await messageService.sendOne({
    to: myphone,
    from: SMS_SENDER,
    text: `[์ฝ”๋“œ์บ ํ”„] ์•ˆ๋…•ํ•˜์„ธ์š”?! ์š”์ฒญํ•˜์‹  ์ธ์ฆ๋ฒˆํ˜ธ๋Š” [${token}] ์ž…๋‹ˆ๋‹ค.`,
  });
  console.log(result);
>
  // console.log(myphone + '๋ฒˆํ˜ธ๋กœ ์ธ์ฆ๋ฒˆํ˜ธ' + token + '๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค!!!');
}

์ด์ œ .env ํŒŒ์ผ์ด ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ์ถ”๊ฐ€์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ์„ค์น˜ํ•œ๋‹ค.

$ yarn add dotenv

index.js ํŒŒ์ผ์—์„œ detenv๋ฅผ importํ•ด์˜จ๋‹ค.

import 'dotenv/config'

๐Ÿ“Œ EMAIL ์ „์†ก

์ด๋ฒˆ์—๋Š” ๋ฌธ์ž์— ์ด์–ด์„œ ์ด๋ฉ”์ผ์„ ์ „์†กํ•ด๋ณด๋Š” api๋ฅผ ๋งŒ๋“ค์–ด ์‹ค์Šต์„ ์ง„ํ–‰ํ–ˆ๋‹ค.

Nodemailer
https://nodemailer.com/about/

Nodemailer๋ฅผ ์‚ฌ์šฉํ•ด ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋ฐ, ์ด๋ฅผ ์œ„ํ•ด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด Nodemailer๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

$ yarn add nodemailer

email api๊ฐ€ ๋งŒ๋“ค์–ด์ง„ email.js ํŒŒ์ผ์—์„œ nodemailer๋ฅผ importํ•˜๊ณ  ๋ฉ”์ผ์„ ์ „์†กํ•œ๋‹ค.

// email.js
>
import nodemailer from 'nodemailer';
>
// ...๊ธฐ์กด ์ฝ”๋“œ
>
export async function sendTemplateToEmail(myemail, mytemplate){
		const EMAIL_USER = "youremail@gmail.com";
	  const EMAIL_PASS = "abcdefghijklmnop";
	  const EMAIL_SENDER = "youremail@gmail.com";
 >   
		const transporter = nodemailer.createTransport({
        service: "gmail",
        auth: {
            user: EMAIL_USER,
            pass: EMAIL_PASS,
        }
    })
>
    const result = await transporter.sendMail({
        from: EMAIL_SENDER,
        to: myemail,
        subject: "[์ฝ”๋“œ์บ ํ”„] ๊ฐ€์ž…์„ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค!!!",
        html: mytemplate
    })
    console.log(result)
>
    // console.log(myemail + "์ด๋ฉ”์ผ๋กœ ๊ฐ€์ž…ํ™˜์˜ํ…œํ”Œ๋ฆฟ " + mytemplate + "๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.")
}

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฌธ์ž์ „์†ก๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •์œผ๋กœ ํ†ตํ•ด ๋ณธ์ธ์˜ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋ฉด api ์ž‘์„ฑ์ด ์™„๋ฃŒ๋œ๋‹ค.

๐ŸŒˆ ์˜ค๋Š˜ ํ•˜๋ฃจ

๊ทธ๋™์•ˆ api๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐ์ดํ„ฐ๋งŒ ์ฃผ๊ณ ๋ฐ›๋Š” ๊ณผ์ •๊นŒ์ง€๋งŒ ์‹ค์Šต์„ ํ–ˆ์ง€๋งŒ, ์˜ค๋Š˜๋ถ€ํ„ฐ ์ง์ ‘ ๋ฌธ์ž๋‚˜ ๋ฉ”์ผ์„ ์ „์†กํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ง์ ‘ ํ•ด๋ณด๋ฉด์„œ ์ฆ๊ฒ๊ฒŒ ๊ณต๋ถ€๋ฅผ ํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ๋‚ด๊ฐ€ ๋งŒ๋“  api๊ฐ€ ์‹ค์ œ๋กœ ๊ธฐ๋Šฅ์„ ํ•˜๊ณ  ๋ˆˆ์œผ๋กœ ํ™•์ธํ•˜๋‹ˆ ๋ฒŒ์จ๋ถ€ํ„ฐ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋‹ค๋ฅธ api๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ณ  ์‹ถ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ํ•˜๋ฃจํ•˜๋ฃจ ์‹ค๋ ฅ์„ ์Œ“์•„๊ฐ€๋ฉฐ ์–ธ์  ๊ฐ€๋Š” ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์€ api๋ฅผ ๋งŒ๋“œ๋Š” ๋‚ ์ด ์™”์œผ๋ฉด ์ข‹๊ฒ ๋‹ค.

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