Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Understanding Multiple Routes | Section
Building APIs with Express.js

bookUnderstanding Multiple Routes

メニューを表示するにはスワイプしてください

An application usually needs more than one route. Each route handles a different URL and returns a different response.

You can define multiple routes in the same server:

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Home page');
});

app.get('/about', (req, res) => {
  res.send('About page');
});

app.get('/contact', (req, res) => {
  res.send('Contact page');
});

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

Each route has its own path. When a request comes in, Express checks the path and runs the matching route.

For example:

  • '/': home page;
  • '/about': about page;
  • '/contact': contact page.

If a route is not defined, Express will not know how to handle the request.

This is how applications serve different content based on the URL.

question mark

What happens when a user opens a URL that does not match any defined route?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  4
some-alt