Menu
CoddyTech

Daha Fazla Rota

Coddy'nin Node.js Journey'sinin Express Temelleri bölümünün bir parçası. Ders 3 / 20.

Bir sitenin birden fazla sayfası vardır, bu nedenle sunucunun her yol için bir rotaya ihtiyacı vardır.

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 her isteğin yolunu okur ve bu yola sahip rotayı çağırır: GET /books ilk işleve, GET /opening-hours ise ikinci işleve ulaşır. Rotası olmayan bir yol, Express'in kendi 404 sayfasını gösterir. Bir rotanın yolu her zaman / ile başlar.

Recipe Box geri döndü; dört tarif içeren bir data.js dosyası ve bu tariflerin başlıklarını listeleyen bir /recipes rotası var.

challenge icon

Görev

Kolay

/about sayfasını ekleyerek Recipe Box collects family recipes. yanıtını ver.

Çalıştır, GET /about isteğini gönder, ardından GET /about-us isteğini gönder ve Express'in listelediği rotaları oku.

Kendin dene

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

const app = express();

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

// type('text') düz metin gönderir, böylece her başlık ayrı bir satırda görünür.
app.get('/recipes', (req, res) => {
    res.type('text').send(recipes.map((recipe) => recipe.title).join('\n'));
});

// /about sayfasını buraya ekleyin.

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

Express Temelleri bölümündeki tüm dersler