์ƒˆ์†Œ์‹

LANGUAGES/Node

[Node] node.js๋ฅผ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„ํ†ต์‹  ๊ตฌํ˜„ํ•˜๊ธฐ

  • -

๐Ÿ“Œ Node.js ๋ฐฑ์—”๋“œ ๊ธฐ์ดˆ๊ฐ•์˜

์ด ๊ธฐ์ดˆ๊ฐ•์˜ ๋‚ด์šฉ์€ ์œ ํŠœ๋ธŒ ์กฐ์ฝ”๋”ฉ JoCoding๋‹˜์˜ "ํ•œ ์‹œ๊ฐ„ ๋งŒ์— Node.js ๋ฐฑ์—”๋“œ ๊ธฐ์ดˆ ๋๋‚ด๊ธฐ(ft. API๊ตฌ์ถ•)"์ด๋ผ๋Š” ์ œ๋ชฉ์„ ๊ฐ€์ง„ Node.js์˜ ๊ธฐ๋ณธ์˜์ƒ์ด๋‹ค. ๊ธธ์ด๋Š” 1์‹œ๊ฐ„ ์ •๋„๋กœ Node.js๊ฐ€ ๋ฌด์—‡์ด๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๋ถ€ํŠธ์บ ํ”„ ์ˆ˜๋ฃŒ์‹ ๊ฐ€๋Š” ์ง€ํ•˜์ฒ ์—์„œ 1์‹œ๊ฐ„ ์ •๋„ ๊ฐ€๋ณ๊ฒŒ ๋ดค์—ˆ๋Š”๋ฐ Node.js๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ข‹์€ ๊ฐ•์˜๋ผ ์ƒ๊ฐ์ด ๋“ค์–ด ๋‹ค์‹œ ํ•œ๋ฒˆ ์‹œ์ฒญํ•˜๋ฉด์„œ ๊ฐ•์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

์ถœ์ฒ˜ : ์กฐ์ฝ”๋”ฉ JoCoding -ํ•œ์‹œ๊ฐ„๋งŒ์— Node.js ๋ฐฑ์—”๋“œ ๊ธฐ์ดˆ ๋๋‚ด๊ธฐ(ft. API๊ตฌ์ถ•)

 

๐Ÿ“Œ Node.js๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

์ง€๊ธˆ ๋ฉ”์ธ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” python ์–ธ์–ด๋Š” ์ปดํ“จํ„ฐ์— ์„ค์น˜ํ•ด์•ผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ๋ฐฑ์—”๋“œ ์–ธ์–ด์ด๋‹ค. ํ•˜์ง€๋งŒ javascript๋Š” ์„ค์น˜๊ณผ์ • ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ ์ด์œ ๋Š” javascript๋Š” ๋ธŒ๋ผ์šฐ์ €์— ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์žˆ์–ด javascript๋ฅผ ์ฝ์–ด ์‹คํ–‰์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ์ปฌ์— ์„ค์น˜ ์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.


๊ทธ๋Ÿฐ๋ฐ ์ด์ œ javascript๋ฅผ node.js ๋ฐฑ์—”๋“œ(์„œ๋ฒ„)์—์„œ ์ž‘๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” node.js๋ฅผ ์„ค์น˜ํ•ด์•ผ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋ ‡๊ฒŒ Node.js๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋„์šธ ํ•„์š” ์—†์ด javascript๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์ด ๋งŒ๋“ค์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋ฐฑ์—”๋“œ(์„œ๋ฒ„)์™€ ํ”„๋กœํŠธ(ํด๋ผ์ด์–ธํŠธ) ๋ชจ๋‘ javascript๋กœ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

์ด๋ ‡๊ฒŒ ๋ฐฑ์—”๋“œ์™€ ํ”„๋ŸฐํŠธ ๋ชจ๋‘ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” node.js๋ฅผ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ•™์Šตํ•˜๊ธฐ ์ „์— node.js๊ฐ€ ๋ฌด์—‡์ด๋ฉฐ ์–ด๋– ํ•œ ์˜ํ–ฅ๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž. ํ˜„์žฌ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์— ์‚ฌ์šฉ๋˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ์–ธ์–ด๋งˆ๋‹ค ๋‹ค์–‘ํ•˜๊ณ  ๋งŽ์€๋ฐ ๊ทธ์ค‘์—์„œ Node.js๋Š” ์ƒ์œ„๊ถŒ์— ์†ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ํ•˜๋‚˜ ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผ ํ•  ๋ถ€๋ถ„์ด node.js๋Š” javascript์ฒ˜๋Ÿผ ๊ฐœ๋ฐœ์— ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ javascript๋กœ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํ•˜๋‚˜์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ฐ€์ž.

 


์ถœ์ฒ˜ : survey.stackoverflow.co
์œ„์˜ ๊ทธ๋ฆผ์€ stackoverflow์—์„œ ์กฐ์‚ฌํ•œ 2022๋…„ Web frameworks์™€ technologies๋ถ€๋ถ„ ํ†ต๊ณ„์ด๋ฉฐ Node.js๊ฐ€ 47%์ด๋ฉฐ ๊ฑฐ์˜ 50%์— ์ž„๋ฐ•ํ•˜๋Š” ๋น„์ค‘์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋งŒํผ ๊ฐœ๋ฐœ์ž๋“ค์ด ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๋Š” ๊ฒƒ์„ ์ˆ˜์น˜๋กœ์„œ ๋ณด์—ฌ์ค€๋‹ค.

 

๐Ÿ“Œ Node.js ์„ค์น˜

ํ˜„์žฌ ์‹œ์ฒญํ•˜๊ณ  ์žˆ๋Š” ๊ฐ•์˜๋Š” Windows๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์„ค์น˜๊ณผ์ •์„ ์„ค๋ช…ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ Mac์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋‚˜๋Š” Homebrew๋ฅผ ์‚ฌ์šฉํ•ด์„œ node.js๋ฅผ ์„ค์น˜ํ•ด ๋ณด๊ฒ ๋‹ค. Homebrew๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜ ๋ธ”๋กœ๊ทธ๋ฅผ ๋”ฐ๋ผ ํ•˜๋ฉด Homebrew์™€ node.js๋ฅผ ์„ค์น˜ํ•˜๋„๋ก ํ•˜์ž.
์ถœ์ฒ˜ : Node.js ์„ค์น˜

 

MAC OS์— node.js ์„ค์น˜ํ•˜๊ธฐ (์‚ฌ์ดํŠธ์—์„œ ์„ค์น˜ ๋˜๋Š” nvm ์„ค์น˜)

๋ณธ ๊ธ€์—์„œ๋Š” node.js ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ• 2๊ฐ€์ง€๋ฅผ ์†Œ๊ฐœํ•œ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ๋Š” "๋ฐฉ๋ฒ• 1" nvm์„ ์ด์šฉํ•˜์—ฌ node.js ๋ฅผ ์„ค์น˜ํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•œ๋‹ค. ๋ฐฉ๋ฒ•1. nvm์œผ๋กœ node.js ์„ค์น˜ nvm ์ด๋ž€? nvm์€ nodejs version manager๋กœ ์‹œ์Šคํ…œ

memostack.tistory.com

 

โžค NVM

์œ„ ๋ธ”๋กœ๊ทธ๋ฅผ ๋”ฐ๋ผ ํ•˜๋‹ค ๋ณด๋ฉด NVM์ด๋ผ๋Š” ๊ด€๋ฆฌ ๋„๊ตฌ๋ฅผ ์„ค์น˜ํ•˜๋Š”๋ฐ "Node Version Manager"๋ผ๊ณ  ํ•˜๋Š” ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•˜๋Š” ๋„๊ตฌ์ด๋‹ค.

 # ๋ช…๋ น์–ด
$ brew install nvm

 

NVM, ์™œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๊ฐ€?

ํ˜‘์—…์„ ํ•  ๋•Œ, ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋™์‹œ์— ์ง„ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ / ํ”„๋ ˆ์ž„์›Œํฌ / ๊ฐœ๋ฐœํˆด์˜ ๋ฒ„์ „ ํ˜ธํ™˜ ๋ฌธ์ œ๋ฅผ ๊ฒช๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. NVM์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ด์ ์ด ์žˆ๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. ์ปดํ“จํ„ฐ์— ๋‹ค์–‘ํ•œ ๋ฒ„์ „์˜ Node.js๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค.
  2. use ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉํ•  Node ๋ฒ„์ „์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์Šค์œ„์นญํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค.
  3. ๋””ํดํŠธ ๋ฒ„์ „์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ / ์„ค์น˜ํ•œ ๋ฒ„์ „๋“ค์˜ ์ „์ฒด ๋ฆฌ์ŠคํŠธ๋ฅผ ํ™•์ธํ•˜๊ฑฐ๋‚˜ / ํ•„์š” ์—†๋Š” ๋ฒ„์ „์„ ์‚ญ์ œํ•˜๋Š” ๋“ฑ ์†Œ์œ„ ๋ฒ„์ „ ๊ด€๋ฆฌ๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค.
  4. ๋ฃจ๋น„์˜ rvm, rbenv๋‚˜ ํŒŒ์ด์ฌ์˜ pyenv์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

 

โžค nvm ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •

 # ๋ช…๋ น์–ด
$ vim ~/.zshenv
 # ํ™˜๊ฒฝ ์„ค์ • ์ฝ”๋“œ
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

 

โžค nvm ์„ค์น˜ ํ™•์ธ

์œ„์˜ ๊ณผ์ •์ด ๋๋‚ฌ๋‹ค๋ฉด nvm์ด ์ œ๋Œ€๋กœ ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์ž.

# ๋ช…๋ น์–ด
% nvm --version

 

โžค nvm์œผ๋กœ node.js์„ค์น˜

์ด์ œ nvm์„ ์ด์šฉํ•˜์—ฌ node.js๋ฅผ ์„ค์น˜ํ•ด ๋ณด์ž. ls-remote๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” node.js๋ฒ„์ „์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๋ช…๋ น์–ด
% nvm ls-remote

v16.8.0
        v16.9.0
        v16.9.1
       v16.10.0
       v16.11.0
       v16.11.1
       v16.12.0
       v16.13.0   (LTS: Gallium)
       v16.13.1   (LTS: Gallium)
       v16.13.2   (LTS: Gallium)
       v16.14.0   (LTS: Gallium)
       v16.14.1   (LTS: Gallium)
       v16.14.2   (LTS: Gallium)
       v16.15.0   (LTS: Gallium)
       v16.15.1   (LTS: Gallium)
       v16.16.0   (LTS: Gallium)
       v16.17.0   (LTS: Gallium)
       v16.17.1   (LTS: Gallium)
       v16.18.0   (LTS: Gallium)
       v16.18.1   (LTS: Gallium)
       v16.19.0   (Latest LTS: Gallium)
        v17.0.0
        v17.0.1
        v17.1.0
        v17.2.0

ํ™•์ธํ•œ ๋ฒ„์ „์—์„œ ์›ํ•˜๋Š” node.js๋ฒ„์ „์„ ์„ ํƒํ•˜์—ฌ install ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๋ช…๋ น์–ด
$ nvm install 17.2.0

 

๐Ÿ“Œ NVM ๊ธฐํƒ€ ๋ช…๋ น์–ด

# node.js ๋ฒ„์ „ ์„ค์น˜ํ•˜๊ธฐ
$ nvm install 0.10
$ nvm install v0.1.2
$ nvm install v8

# node ์ตœ์‹  ๋ฒ„์ „ ์„ค์น˜ (์„ค์น˜ ๋‹น์‹œ ๊ธฐ์ค€)
$ nvm install node

# node LTS ์ตœ์‹ ๋ฒ„์ „ ์„ค์น˜
$ nvm install --lts
# ์„ค์น˜๋œ node.js ๋ชฉ๋ก ํ™•์ธํ•˜๊ธฐ
$ nvm ls

# ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  Node ๋ฒ„์ „ ์กฐํšŒ (์žฌ๋ฏธ์‚ผ์•„ ํ•ด๋ณด์ง€๋งˆ์„ธ์š” ๊ฒ๋‚˜๋งŽ์Œ... ํ™ฉ๊ธ‰ํžˆ control C ๋‘๋“œ๋ฆฌ๊ธฐ)
$ nvm ls-remote

# ํŠน์ • ๋ฒ„์ „์˜ node ์‚ฌ์šฉํ•˜๊ธฐ
$ nvm use <version>

# ํ˜„์žฌ ์‚ฌ์šฉ์ค‘์ธ ๋ฒ„์ „ ํ™•์ธํ•˜๊ธฐ
$ nvm current

# node.js ์„ค์น˜ ๊ฒฝ๋กœ ํ™•์ธํ•˜๊ธฐ
$ which node

# ํ•„์š”์—†๋Š” node ๋ฒ„์ „ ์‚ญ์ œํ•˜๊ธฐ
$ nvm uninstall <version>

๋งŒ์•ฝ์— ์ƒˆ๋กœ์šด ์‰˜์„ ์‹คํ–‰ํ•  ๊ฒฝ์šฐ node์˜ ๋ฒ„์ „์ด system๋ฒ„์ „์œผ๋กœ ๋ฆฌ์…‹๋˜๋Š”๋ฐ, ์ด๋ฅผ ๊ณ ์ •ํ•˜๊ธฐ ์œ„ํ•œ ์ปค๋งจ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

$ nvm alias default 8.9.4

# ์„ค์น˜๋˜์–ด ์žˆ๋Š” ๊ฐ€์žฅ ์ตœ์‹ ๋ฒ„์ „์˜ node๋ฅผ ๋””ํดํŠธ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ
$ nvm alias default node

 

๐Ÿ“Œ ๊ธฐ์กด javascript๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ํ™˜๊ฒฝ

์šฐ๋ฆฌ๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์—์„œ javascript๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋„์›Œ์ง„ ์ƒํƒœ์—์„œ V8์ด๋ผ๊ณ  ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์ด javascript ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ธํ„ฐํ”„๋ฆฌํ„ฐ(ํ•ด์„๊ธฐ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด์ œ ๋กœ์ปฌ ์ปดํ“จํ„ฐ์—์„œ๋Š” ๊ทธ๋Ÿฐ ํ™˜๊ฒฝ์ด ๊ฐ–์ถฐ์ ธ์žˆ์ง€ ์•Š๋‹ค๊ฐ€ ์ด์ œ Node.js๋ฅผ ์„ค์น˜ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

๐Ÿ“Œ Node.js๋ฅผ ์ด์šฉํ•˜์—ฌ javascript ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ

// index.js

console.log("Hello world!");

์œ„์™€ ๊ฐ™์ด index.jsํŒŒ์ผ์„ ์ƒ์„ฑ ํ›„ ๊ฐ„๋‹จํ•œ javascript์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์‹คํ–‰์„ ์‹œ์ผœ ๋ณด์•˜๋‹ค. node.js ์„ค์น˜ ์—†์ด ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ htmlํŒŒ์ผ์„ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋„์šด ๋‹ค์›€์— jsํŒŒ์ผ์„ import ๋ฐ›์•„ javascript์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ htmlํŒŒ์ผ์— script์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์‹คํ–‰์‹œ์ผœ ์ฃผ์–ด์•ผ ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด์ œ ์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅํ•œ ์ฝ”๋“œ๋ฅผ control + option + n๋‹จ์ถ•ํ‚ค๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ teminal์— ์•„๋ž˜์™€ ๊ฐ™์ด node ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

# ๋ช…๋ น์–ด
% node index.js
# ๊ฒฐ๊ณผ
[Running] node "/Users/cjy/Desktop/Source/Jupyter/node/index.js"
Hello world!

[Done] exited with code=0 in 0.106 seconds

์œ„์™€ ๊ฐ™์ด ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์˜€๋‹ค๋ฉด ์ด์ œ node.js์—์„œ javascript๋ฅผ ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋๋‚œ ๊ฒƒ์ด๋‹ค.

 

๐Ÿ“Œ npm์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ?


์ถœ์ฒ˜ : npm ์‚ฌ์ดํŠธ
npm์ด๋ž€ "Node Package Manage"๋ผ๊ณ  ํ•˜๋Š” node ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž๋ผ๊ณ  ํ•˜๋Š” ํ•„์š”ํ•œ ํˆด์„ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ์ด๋‹ค.


์ถœ์ฒ˜ : ๋ชจ๋“ˆ ๊ฐœ์ˆ˜ ํ†ต๊ณ„
node์— ์ด๋ฏธ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“ˆ์€ ์ˆ˜๋„ ์—†์ด ๋งŽ์€๋ฐ ์•„๋ž˜ ํ†ต๊ณ„๋ฅผ ๋ณด๋ฉด npm์˜ ๋ชจ๋“ˆ ๊ฐœ์ˆ˜๊ฐ€ ์••๋„์ ์œผ๋กœ ๋งŽ์€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

โžค figlet ์‚ฌ์šฉํ•˜๊ธฐ (์ž„์˜ ๋ชจ๋“ˆ ํ•™์Šต-๊ฑด๋„ˆ๋›ฐ์–ด๋„ ๋ฌด๊ด€)

figlet์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ…์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ทธ๋ฆผ์„ ํ‘œํ˜„ํ•˜๋Š” "์•„์Šคํ‚ค ์•„ํŠธ"๋ผ๋Š” ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋‹ค. ํ•ด๋‹น ์„ค๋ช…์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์„ค์น˜ํ•˜๋Š” ๋ช…๋ น์–ด์™€ ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ ์ฝ”๋“œ๊ฐ€ ๋‚˜์™€ ์žˆ์œผ๋‹ˆ ๋”ฐ๋ผ ํ•ด ๋ณด์ž.

์•„๋ž˜์™€ ๊ฐ™์ด index.jsํŒŒ์ผ์— ์ž…๋ ฅ ํ›„ ์‹คํ–‰์‹œ์ผœ ๋ณด๋ฉด ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์„ ์•„์Šคํ‚ค ์•„ํŠธ๋กœ ์ถœ๋ ฅํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

var figlet = require('figlet');

figlet('Hello World!!', function(err, data) {
    if (err) {
        console.log('Something went wrong...');
        console.dir(err);
        return;
    }
    console.log(data)
});
[Running] node "/Users/cjy/Desktop/Source/Jupyter/node/index.js"
  _   _      _ _         _   _           _         _      __        __         _     _ _ _ 
 | | | | ___| | | ___   | \ | | ___   __| | ___   (_)___  \ \      / /__  _ __| | __| | | |
 | |_| |/ _ \ | |/ _ \  |  \| |/ _ \ / _` |/ _ \  | / __|  \ \ /\ / / _ \| '__| |/ _` | | |
 |  _  |  __/ | | (_) | | |\  | (_) | (_| |  __/_ | \__ \   \ V  V / (_) | |  | | (_| |_|_|
 |_| |_|\___|_|_|\___/  |_| \_|\___/ \__,_|\___(_)/ |___/    \_/\_/ \___/|_|  |_|\__,_(_|_)
                                                |__/                                       

[Done] exited with code=0 in 0.145 seconds

 

โžค ๋‹ค์šด๋กœ๋“œํ•œ ๋ชจ๋“ˆ ์ •๋ฆฌ

์‚ฌ์šฉ์ž๊ฐ€ npm์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•œ ๋ชจ๋“ˆ์„ ์ •๋ฆฌํ•ด ์ฃผ๋Š” ๋ช…๋ น์–ด์ด๋ฉฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด package.jsonํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. ๋˜ํ•œ npm install [๋ชจ๋“ˆ๋ช…]์œผ๋กœ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜๊ณ  ๋‚˜๋ฉด [ํ”„๋กœ์ ํŠธ ํด๋”๋ช…]_modules๋ผ๋Š” ํด๋”์™€ package-lock.json์ด ์ƒˆ๋กœ ์ƒ์„ฑ์ด ๋˜๋Š”๋ฐ package.json์€ ํŒŒ์ผ์˜ ๊ฐ„๋žตํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค๋ฉด package-lock.json์€ ๋‹ค์šด๋กœ๋“œํ•œ ๋ชจ๋“ˆ์˜ ์ƒ์„ธ ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ์ด๋‹ค.

# ๋ช…๋ น์–ด
npm init
// package.json
{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

 

๐Ÿ“Œ Express๋ž€ ๋ฌด์—‡์ธ๊ฐ€?


Node.js ๊ธฐ๋ฐ˜์˜ ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด Express๋ชจ๋“ˆ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์œ„์™€ ๊ฐ™์ด ์™ผ์ชฝ์˜ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•œ request๋ฅผ db์™€ ๊ทธ ์™ธ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„ ์›น๋ธŒ๋ผ์šฐ์ €๋กœ ์‘๋‹ต response๋ฅผ ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์„ ์›น ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์—ญํ• ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ด Express์ด๋‹ค.

 

โžค express ์‚ฌ์šฉํ•˜๊ธฐ

# ๋ช…๋ น์–ด
npm install express
// index.js
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

์ถœ์ฒ˜ : expressjs

์œ„์™€ ๊ฐ™์ด express๋ฅผ ์„ค์น˜ํ•œ ํ›„ npm์‚ฌ์ดํŠธ์— ์ ํ˜€์žˆ๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ index.jsํŒŒ์ผ์— ๋ถ™์—ฌ ์ค€ ํ›„ ์‹คํ–‰์„ ์‹œ์ผœ๋ณด์ž. ์ฒ˜์Œ์—๋Š” ๋ฐ˜์‘์ด ์—†์„ ๊ฒƒ์ด๋‹ค. ์ผ๋‹จ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์ „์— ๋ธŒ๋ผ์šฐ์ €์—์„œ localhost:3000๋กœ url์„ ์ž…๋ ฅํ•˜์—ฌ ์ด๋™ํ•ด ๋ณด์ž. ๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

โžค require

const express = require('express')
const app = express()

์œ„์˜ ๋‚ด์šฉ์€ require์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์น˜ํ•œ express๋ผ๋Š” ๋ชจ๋“ˆ์„ "express"๋ผ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ด ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.

โžค port / listen

const port = 3000

app.listen(port, () => {
        console.log(`Example app listening on port ${port}`)
})

3000์ด๋ผ๋Š” ๋ฒˆํ˜ธ๋กœ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํฌํŠธ๋ผ๋Š” ๊ฒƒ์„ ์—ด์–ด์ฃผ๊ธฐ ์œ„ํ•ด ํฌํŠธ๊ฐ’์„ ์—ด์–ด ์ค€ ๊ฒƒ์ด๋‹ค.
app.listen์ด๋ผ๋Š” ๊ฒƒ์„ ํ†ตํ•ด ๊ทธ ์„ค์ •ํ•œ ๊ฐ’์„ ๋ฐ›์€ ๊ฒƒ์ด๋‹ค. ์ด listen์€ ํ•ญ์ƒ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ์ƒํƒœ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•˜์ž.

โžค get

app.get('/', (req, res) => {
  res.send('Hello World!')
})

์œ„์—๋Š” expressํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ app์ด๋ผ๋Š” ๊ฐ์ฒด์— get์„ ์‚ฌ์šฉํ•˜์˜€๊ณ , ์ด ์•ˆ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ '/'์™€ (req, res) => { res.send('Hello World!') } ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด ์ฃผ์—ˆ๋‹ค.

  1. get (HTTP ๋ฉ”์„œ๋“œ)
  • HTTP ๋ฉ”์„œ๋“œ๋กœ์„œ ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋ฉฐ get, post, delete, put... ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
  1. '/' (๋ผ์šฐํŒ…)
  • ์œ„์—์„œ ์„ค๋ช…ํ•œ ์—ด์–ด์ค€ port๋กœ ๋“ค์–ด์˜ค๋Š” ๋‹ค์–‘ํ•œ api ๊ฒฝ๋กœ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  1. (req, res) => { res.send('Hello World!') } (์ฝœ๋ฐฑํ•จ์ˆ˜)
 

javascript-CallBack Function ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด์ž

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ž€? ์ •์˜ ํ•จ์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ผ ๋ถ€๋ฅธ๋‹ค. ์ด๋ ‡๊ฒŒ๋งŒ ์ด์•ผ๊ธฐํ•ด๋„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋‹น์‹ ์€ ์ดˆ๋ณด์ž๊ฐ€ ์•„๋‹ ๊ฒƒ์ด๋‹ค. ์šฉ๋„ javascri

laagom.tistory.com

์•„๊นŒ ์œ„์—์„œ localhost:3000์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์— ์ž…๋ ฅ์„ ํ•˜์—ฌ 'Hello World!'๊ฐ€ ๋‚˜์˜ค๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™์„ ํ•œ ๊ฒƒ์„ ๋ณด์•˜๋Š”๋ฐ ์ด์ œ ์œ„์˜ ์ฝ”๋“œ๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์— ์–ด๋– ํ•œ ์—ญํ• ์„ ํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๐Ÿ“Œ api ๋ณ„๋กœ ๋ถ„๊ธฐํ•˜์—ฌ ๋ผ์šฐํŒ… ์ƒ์„ฑ

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.send('Hello World!')
})

app.get('/dog', (req, res) => {
    res.send('๋ฉ๋ฉ')
})

app.get('/cat', (req, res) => {
    res.send('<h1>์•ผ์˜น</h1>')
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ผ์šฐํŒ…์„ ๋‚˜๋ˆ  ๋ณด์•˜๋‹ค.

  • '/'๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ๋Š” "Hello World!"
  • '/dog'๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ๋Š” "๋ฉ๋ฉ"
  • '/cat'๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ๋Š” "์•ผ์˜น'"

Point!!

'/cat'๋ผ์šฐํŒ…์—์„œ htmlํ˜•์‹์˜ ๋ฌธ์ž์—ด์„ ๋ณด๋‚ด๋ฉด ํ™”๋ฉด์— h1 ํƒœ๊ทธ๊ฐ€ ์ ์šฉ๋˜๋ฉฐ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” res.send()์— html๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“Œ response์— ๋ณ€์ˆ˜๋ฅผ ๋‹ด์•„ ๋ณด๋‚ด๊ธฐ

โžค GET: params, query


์œ„์˜ ๊ทธ๋ฆผ์˜ ๋ธŒ๋ผ์šฐ์ € ๊ฒฝ๋กœ๋ฅผ ๋ณด๋ฉด youtube.com/[์œ ํŠœ๋ธŒ ๊ณ„์ • ๋ช…์นญ]์œผ๋กœ ์ฃผ์†Œ๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ ํŠœ๋ธŒ๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ ๋ชจ๋“  ์œ ํŠœ๋ธŒ ๊ณ„์ •์— ์œ„์™€ ๊ฐ™์ด api๋ฅผ ๋งŒ๋“ค์ง€๋Š” ์•Š์•˜์„ ๊ฒƒ์ด๋‹ค. ํ•ด๋‹น ๊ณ„์ •์— ์ ‘์†ํ•˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ์ด๋ฃจ์–ด์กŒ์„ ๋•Œ ํ•ด๋‹นํ•˜๋Š” ๊ณ„์ • ๋ช…์นญ์˜ id๊ฐ’์ด ๋ผ์šฐํŒ…์— param์œผ๋กœ ๋ณด๋‚ด์กŒ๊ณ  ๋™์ผํ•œ api๋กœ ๋“ค์–ด์™€ param๊ฐ’์œผ๋กœ ์กฐํšŒ๋˜๋Š” ๋กœ์ง์„ ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ–ˆ์„ ๊ฑฐ๋ผ๊ณ  ์˜ˆ์ƒํ•œ๋‹ค
๊ทธ๋Ÿฌ๋ฉด ์šฐ๋ฆฌ๋Š” ๋ธŒ๋ผ์šฐ์ € url์— param์œผ๋กœ id๋ฅผ ๋ณด๋‚ด๊ณ  param๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋˜๋Š” api ๋ผ์šฐํŒ…์„ ์ž‘์„ฑํ•ด ๋ณด์ž.

โžค param

const express = require('express')
const app = express()
const port = 3000

app.get('/user/:id', (req, res) => {
    const param = req.params
    console.log(param)
    console.log(param.id)

    res.json({'animal': param.id})
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

์œ„์˜ ๋กœ์ง์€ '/user/:id'์™€ ๊ฐ™์ด ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•˜์—ฌ [id]์— ๋Œ€ํ•œ param ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ์ž‘์„ฑํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ›์€ param์„ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๊ฒŒ param.id๋กœ param์— ๋‹ด๊ฒจ์žˆ๋Š” id ๊ฐ’์„ ๊บผ๋‚ด์„œ jsonํ˜•์‹์œผ๋กœ response์— ๋ณด๋‚ด์ฃผ์—ˆ๋‹ค.

# ๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œ
http://localhost:3000/user/๊ณ ์–‘์ด

์™€ ๊ฐ™์ด ๋ธŒ๋ผ์šฐ์ €์— ์ฃผ์†Œ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. /user/:id ๋’ค์— ๋ถ™์€ id๋ผ๋Š” param๊ฐ’์ด api๋ฅผ ๊ฑฐ์ณ์„œ ์ถœ๋ ฅ๋˜๊ฒŒ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋‹ค. ๋ฌผ๋ก  ํ•ด๋‹น api์—์„œ db์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š์ง€๋งŒ param์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„์ด ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€๋‹ค.

# ๊ฒฐ๊ณผ
{"animal":"๊ณ ์–‘์ด"}

 

โžค query

์œ„์—์„œ๋Š” param์„ ์ด์šฉํ•ด์„œ ๋ผ์šฐํŒ…์„ ์ด์šฉํ–ˆ๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” query๋ฅผ ์ด์šฉํ•˜์—ฌ request๋กœ ์š”์ฒญํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด๊ฒ ๋‹ค.

const express = require('express')
const app = express()
const port = 3000

app.get('/user/query', (req, res) => {
    const query = req.query
    console.log(query)

    res.json(query)
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

์œ„์™€ ๊ฐ™์ด ๋ณด๋‚ผ ๋•Œ๋งˆ๋‹ค param๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ query๋ฅผ ํ†ตํ•ด์„œ ๊ฒฐ๊ด๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์ถœ๋ ฅ์„ ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์•˜๋‹ค.

http://localhost:3000/user/query?name=Lagom&age=31&addr=Seoul

์œ„์™€ ๊ฐ™์ด /user/query๋กœ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•˜๊ณ  ๋’ค์—? name=Lagom&age=31&addr=Seoul์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ณด๋‚ด๊ฒŒ ๋˜๋ฉด ํ™”๋ฉด์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด url๋กœ ๋ณด๋‚ธ query๊ฐ’์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

{"name":"Lagom","age":"31","addr":"Seoul"}

 

๐Ÿ“Œ name์— ๋”ฐ๋ผ ๋‹ค๋ฅธ sound๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ

โžค GET /sound/:name

์ด์ „์—๋Š” ์ด๋ ฅ ํ•œ url์˜ param๊ฐ’, ์ฆ‰ id๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด ์ž…๋ ฅํ•œ id๊ฐ’์ด ์ถœ๋ ฅ๋˜๊ฒŒ ๊ตฌํ˜„์„ ํ•˜์˜€๋‹ค. ์ด๋ฒˆ์—๋Š” ์ „๋‹ฌ๋ฐ›์€ param๊ฐ’์— ๋”ฐ๋ผ ์ถœ๋ ฅ๋˜๋Š” ๊ฐ’์„ param๊ฐ’์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณด์ด๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ๋‹ค.

const express = require('express')
const app = express()
const port = 3000

app.get('/sound/:name', (req, res) => {
    const { name } = req.params

    if(name == 'dog') {
        res.json('๋ฉ๋ฉ')
    }else if(name == 'cat') {
        res.json('์•ผ์˜น')
    }else if(name == 'pig') {
        res.json('๊ฟ€๊ฟ€')
    }else{
        res.json('์•Œ์ˆ˜ ์—†์Œ!!')
    }
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

์œ„์™€ ๊ฐ™์ด ์ „๋‹ฌ๋ฐ›์€ param์— ๋Œ€ํ•œ name๊ฐ’์„ ๋ณ€์ˆ˜๋กœ ํ• ๋‹นํ•˜์—ฌ ๋ณ€์ˆ˜๋กœ dog, cat, pig, ๊ทธ ์™ธ๋กœ ํ•˜์—ฌ ๋ถ„๊ธฐ๋ฅผ ๊ฑธ์–ด ๊ฒฐ๊ด๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์ถœ๋ ฅํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

http://localhost:3000/sound/dog

http://localhost:3000/sound/cat

http://localhost:3000/sound/pig

http://localhost:3000/sound/๋‚˜

์œ„์™€ ๊ฐ™์ด ๊ฐ๊ฐ ๋‹ค๋ฅธ url์„ ๋ธŒ๋ผ์šฐ์ €์— ์ž…๋ ฅํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด๋‹นํ•˜๋Š” ๋™๋ฌผ์— ๋Œ€ํ•œ ์šธ์Œ์†Œ๋ฆฌ๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค.

"๋ฉ๋ฉ"

"์•ผ์˜น"

"๊ฟ€๊ฟ€"

"์•Œ์ˆ˜์—†์Œ!!"

 

๐Ÿ“Œ CORS์ด์Šˆ

HTMLํŒŒ์ผ์—์„œ node.js ๋˜๋Š” ๋‹ค๋ฅธ ์„œ๋ฒ„์— request๋ฅผ ์š”์ฒญ์„ ๋ณด๋ƒˆ์„ ๋•Œ ์„œ๋ฒ„์—์„œ๋Š” ์ด์ƒํ•œ ๊ณณ ๋˜๋Š” ์œ„ํ—˜ํ•œ ์š”์ฒญ์ด ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์œผ๋กœ ์š”์ฒญ์„ ๋ง‰๊ฒŒ ๋˜์–ด ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ HTML๋กœ ์š”์ฒญ์„ ํ•  ๋•Œ CORS์„ค์ •์ด ์—†์œผ๋ฉด ์ฐจ๋‹จ์ด ๋œ๋‹ค. ์ด๊ฒƒ์€ django์—์„œ staticํŒŒ์ผ์„ s3๋กœ storages๋ฅผ ์—ฐ๊ฒฐํ•  ๋•Œ๋„ ๋ฐœ์ƒํ–ˆ๋˜ ์ด์Šˆ์ด๋‹ค. ์ด ์ด์Šˆ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๊ฒƒ์€ ํ”„๋กœ์ ํŠธ ์ด์Šˆ์‚ฌํ•ญ์„ ์ •๋ฆฌํ•  ๋•Œ ๋‹ค์‹œ ๋‹ค๋ค„ ๋ณด๊ฒ ๋‹ค.

 

๐Ÿ“Œ ํ”„๋ŸฐํŠธ ์—”๋“œ CORS ์—ฐ๊ฒฐ

โžค COSR ๋ชจ๋“ˆ ์„ค์น˜ ๋ฐ ์—ฐ๊ฒฐ

# ๋ช…๋ น์–ด
npm install cors
const express = require('express')
const cors = require('cors')
const app = express()
const port = 3000

app.use(cors())

์œ„์™€ ๊ฐ™์ด require('cors')๋กœ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•œ ์ˆ˜ app.user(cors())๋กœ ํ•˜์—ฌ cors์—ฐ๊ฒฐ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

โžค HTML request์š”์ฒญ ํ™”๋ฉด ๊ตฌํ˜„

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cors ์ด์Šˆ ํ•ด๊ฒฐ</title>
</head>
<body>
    <input type="text" id="name">
    <button onclick="getSound()">API ์š”์ฒญ</button>
    <script>
        function getSound() {
            const name = document.getElementById('name').value
            fetch(`http://localhost:3000/sound/${name}`)
            .then(response => response.json())
            .then(data => {
                console.log(data)
                document.getElementById('name').value = data
            })
        }
    </script>
</body>
</html>

์œ„์™€ ๊ฐ™์ด input๋ฐ•์Šค์— dog, cat, pig๊ฐ’์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ ์•„๊นŒ ์œ„์—์„œ ์ž‘์„ฑํ•œ ์„œ๋ฒ„ api๋กœ fetchํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์š”์ฒญ์„ ๋ณด๋‚ด๊ฒŒ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. dog, cat, pig๊ฐ’์— ๋”ฐ๋ผ ๋‚˜์˜ค๋Š” ์šธ์Œ์†Œ๋ฆฌ๊ฐ€ ๋‹ค์‹œ input๋ฐ•์Šค์— ๊ฐ’์ด ์ž…๋ ฅ๋˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

์œ„์™€ ๊ฐ™์ด cat์„ ์ž…๋ ฅํ•œ ํ›„ API ์š”์ฒญ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๊ตฌํ˜„ํ–ˆ๋˜ ๋ผ์šฐํŒ…์— ๋Œ€ํ•œ api ์„œ๋ฒ„๋‹จ์„ ๊ฑฐ์ณ์„œ ์•ผ์˜น ๊ฐ’์„ ๋ฐ›์•„ input์— ๋žœ๋” ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‹ค๋ฅธ HTML์—์„œ ์š”์ฒญ์„ ๋ณด๋‚ด๋„ CORS์ด์Šˆ๊ฐ€ ๋ฐœ์ƒ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€๋‹ค.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.