Menu
CoddyTech

더 많은 라우트

Coddy Node.js 여정의 Express 기초 섹션에 포함된 레슨. 20개 중 3번째.

사이트에는 페이지가 여러 개 있으므로 서버는 각 경로에 대한 라우트가 필요합니다.

app.get('/books', (req, res) => {
    res.send('12 books on the shelf');
});

app.get('/opening-hours', (req, res) => {
    res.send('Monday to Friday, 10 to 6');
});

Express는 각 요청의 경로를 읽고 해당 경로의 라우트를 호출합니다. GET /books는 첫 번째 함수에 도달하고, GET /opening-hours는 두 번째 함수에 도달합니다. 라우트가 없는 경로에는 Express 자체의 404 페이지가 표시됩니다. 라우트의 경로는 항상 /로 시작합니다.

Recipe Box가 다시 등장합니다. 네 개의 레시피가 들어 있는 data.js 파일과 레시피 제목을 나열하는 /recipes 라우트가 있습니다.

challenge icon

챌린지

쉬움

/about 페이지를 추가하여 Recipe Box collects family recipes.라고 응답하게 하세요.

실행한 다음 GET /about을 보내고, 이어서 GET /about-us를 보낸 후 Express가 나열하는 경로를 읽으세요.

직접 해보기

import express from 'express';
import { recipes } from './data.js';

const app = express();

app.get('/', (req, res) => {
    res.send('Welcome to Recipe Box');
});

// type('text')는 일반 텍스트를 보내므로 각 제목이 별도의 줄에 표시됩니다.
app.get('/recipes', (req, res) => {
    res.type('text').send(recipes.map((recipe) => recipe.title).join('\n'));
});

// 여기에 /about 페이지를 추가하세요.

app.listen(3000, () => {
    console.log('Recipe Box is running on port 3000');
});

Express 기초의 모든 레슨