1. Express

ํ‚ค์›Œ๋“œ

  • Express ๋ž€

  • URL ๊ตฌ์กฐ

  • REST API

  • HTTP Method(CRUD)

Express

Node.js ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

Node.js

  • V8์—”์ง„์œผ๋กœ ๋นŒ๋“œ๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋Ÿฐํƒ€์ž„

    • ๋Ÿฐํƒ€์ž„ : ํŠน์ • ์–ธ์–ด๋กœ ๋งŒ๋“  ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ

  • ๋‹ค์–‘ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ(์ฃผ๋กœ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰)

Express

  • Node.js์—์„œ ํšจ์œจ์ ์œผ๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

  • HTTP ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ฉ”์†Œ๋“œ ๋ฐ ๋ฏธ๋“ค์›จ์–ด๋กœ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ API๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ

ts-node

.ts ํŒŒ์ผ์„ ๋”ฐ๋กœ ์ปดํŒŒ์ผํ•˜์ง€ ์•Š๊ณ  node.js์—์„œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋„๊ตฌ

Express ํ™˜๊ฒฝ ์„ค์ •

mkdir express-demo-app
cd mkdir express-demo-app

.gitignore ์„ธํŒ…

touch .gitignore
echo "/node_modules/" > .gitignore

ํŒจํ‚ค์ง€ ์ดˆ๊ธฐํ™”

npm init -y

TypeScript

npm i -D typescript
npx tsc --init

ESLint

npm i -D eslint
npx eslint --init

ts-node

npm init -y

Express ์„ค์น˜

npm i express
npm i -D @types/express

Hello World ์˜ˆ์ œ

app.ts http://localhost:3000/์œผ๋กœ GET์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด Hello, world!๋ฅผ ์‘๋‹ต

import express from 'express';

const port = 3000;

const app = express();

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

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

ts-node๋กœ ์‹คํ–‰

npx ts-node app.ts

์ฝ”๋“œ ์ˆ˜์ •์‹œ ์ž๋™์œผ๋กœ ๋ฐ˜์˜๋˜๋„๋ก nodemon ์‚ฌ์šฉ

npm i -D nodemon
npx nodemon app.ts

package.json script ์„ธํŒ…

  "scripts": {
    "start": "npx nodemon app.ts",
    "lint": "eslint --fix ."
  },

REST API

  • URI๋Š” ์ •๋ณด์˜ ์ž์›์„ ํ‘œํ˜„

    • /write-post ์ฒ˜๋Ÿผ ๋™์ž‘์„ ํ‘œํ˜„ํ•˜์ง€ ์•Š๊ณ  /post ์ฒ˜๋Ÿผ ์ž์›๋งŒ ๋ช…์‹œ

  • ์ž์›์— ๋Œ€ํ•œ ํ–‰์œ„๋Š” HTTP Method๋กœ ํ‘œํ˜„(GET, POST, PUT, DELETE)

    • Collection(๋ณต์ˆ˜)๊ณผ Item(Element)(๋‹จ์ˆ˜)๋กœ ๋‚˜๋‰จ

    • Read (Collection) โ†’ GET /products โ‡’ ์ƒํ’ˆ ๋ชฉ๋ก ํ™•์ธ

    • Read (Item) โ†’ GET /products/{id} โ‡’ ํŠน์ • ์ƒํ’ˆ ์ •๋ณด ํ™•์ธ

    • Create (Collection Pattern ํ™œ์šฉ) โ†’ POST /products โ‡’ ์ƒํ’ˆ ์ถ”๊ฐ€ (JSON ์ •๋ณด ํ•จ๊ป˜ ์ „๋‹ฌ)

    • Update (Item) โ†’ PUT ๋˜๋Š” PATCH /products/{id} โ‡’ ํŠน์ • ์ƒํ’ˆ ์ •๋ณด ๋ณ€๊ฒฝ (JSON ์ •๋ณด ํ•จ๊ป˜ ์ „๋‹ฌ)

    • Delete (Item) โ†’ DELETE /products/{id} โ‡’ ํŠน์ • ์ƒํ’ˆ ์‚ญ์ œ

Thinking in React ์˜ˆ์ œ

app.get('/products', (req, res) => {

  const products = [
    {
      category: 'Fruits', price: '$1', stocked: true, name: 'Apple',
    },
    {
      category: 'Fruits', price: '$1', stocked: true, name: 'Dragonfruit',
    },
    {
      category: 'Fruits', price: '$2', stocked: false, name: 'Passionfruit',
    },
    {
      category: 'Vegetables', price: '$2', stocked: true, name: 'Spinach',
    },
    {
      category: 'Vegetables', price: '$4', stocked: false, name: 'Pumpkin',
    },
    {
      category: 'Vegetables', price: '$1', stocked: true, name: 'Peas',
    },
    ];

  res.send({ products });
});

http://localhost:3000/products ์ ‘์†ํ•˜์—ฌ ํ™•์ธ

CommandLine์œผ๋กœ ํ™•์ธ

curl localhost:3000/products

http localhost:3000/products

Last updated