0Pricing
Node.js Backend Development Bootcamp · Lekcja

Obsługa danych żądania: body, query i params

Proszę poznać sposób, w jaki Express odbiera i analizuje dane przychodzące od klientów, w tym parametry tras, ciągi zapytań, treści JSON i dane z formularzy.

Obsługa danych żądania: body, query i params to bezpłatna lekcja Node.js Backend Development Bootcamp na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Node.js Backend Development Bootcamp, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Node.js Backend Development Bootcamp zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Where Client Data Comes From

An HTTP request can carry data in several places. An Express handler reads each from a different property of the req object:

  • Route params: req.params
  • Query string: req.query
  • Request body: req.body
  • Headers: req.headers
app.get('/info', (req, res) => {
  res.json({ params: req.params, query: req.query });
});

Route Parameters

Route parameters are named URL segments prefixed with a colon. For /users/:id, a request to /users/42 gives req.params.id === '42'.

They are always strings, so convert to numbers when needed.

app.get('/users/:id', (req, res) => {
  const id = Number(req.params.id);
  res.send('Looking up user ' + id);
});

Multiple Route Parameters

A route can capture several params at once. Each colon segment becomes a key on req.params.

app.get('/books/:author/:title', (req, res) => {
  const { author, title } = req.params;
  res.send(author + ' wrote ' + title);
});

Query Strings

Everything after the ? in a URL is the query string. Express parses it into req.query as key-value pairs.

For /search?term=node&page=2, you get req.query.term and req.query.page.

app.get('/search', (req, res) => {
  const { term, page } = req.query;
  res.send('Searching ' + term + ' page ' + (page || 1));
});

Parsing JSON Bodies

POST and PUT requests usually send a JSON body. Express does not parse it by default — you must enable the built-in middleware express.json().

const express = require('express');
const app = express();
app.use(express.json());
app.post('/users', (req, res) => {
  res.json({ received: req.body });
});

Parsing Form Data

HTML forms submit data as application/x-www-form-urlencoded. Enable express.urlencoded() to populate req.body from form fields.

app.use(express.urlencoded({ extended: true }));
app.post('/login', (req, res) => {
  res.send('Welcome ' + req.body.username);
});

Validating Required Fields

Never trust client input. Always check that required fields are present before using them, and respond with a 400 Bad Request if something is missing.

app.post('/users', (req, res) => {
  const { name, email } = req.body;
  if (!name || !email) {
    return res.status(400).json({ error: 'name and email required' });
  }
  res.status(201).json({ name, email });
});

Reading Headers

Headers carry metadata like authentication tokens and content types. Access them via req.headers (keys are lowercased) or the helper req.get().

app.get('/secure', (req, res) => {
  const auth = req.get('Authorization');
  res.send('Token: ' + (auth || 'none'));
});

Default Values for Optional Data

Query and body values are often optional. Provide sensible defaults so your handler does not break when a parameter is omitted.

app.get('/products', (req, res) => {
  const page = Number(req.query.page) || 1;
  const limit = Number(req.query.limit) || 10;
  res.json({ page, limit });
});

Combining All Three Sources

A single endpoint can read params, query, and body together. For example, updating a resource identified by a param, with options in the query, and new data in the body.

app.put('/users/:id', (req, res) => {
  const id = req.params.id;
  const notify = req.query.notify === 'true';
  const updates = req.body;
  res.json({ id, notify, updates });
});

Common Mistakes

Watch out for these pitfalls:

  • Forgetting express.json() so req.body is undefined
  • Treating params as numbers without converting
  • Trusting input without validation
  • Confusing query (?key=value) with params (/:key)

Quick Check

Test your knowledge of Express request data.

Recap

You now know how Express receives client data:

  • req.params for named URL segments
  • req.query for the query string
  • req.body for JSON (with express.json()) and forms (with express.urlencoded())
  • req.headers / req.get() for metadata

Always validate and provide defaults — never trust raw client input.

Często zadawane pytania

Czy lekcja „Obsługa danych żądania: body, query i params” jest bezpłatna?

Tak — pełny tekst „Obsługa danych żądania: body, query i params” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Node.js Backend Development Bootcamp, przejdź na CoddyKit PRO. Kurs Node.js Backend Development Bootcamp zawiera 4 lekcji w sumie.

Co nauczysz się w „Obsługa danych żądania: body, query i params”?

Proszę poznać sposób, w jaki Express odbiera i analizuje dane przychodzące od klientów, w tym parametry tras, ciągi zapytań, treści JSON i dane z formularzy. Ćwiczysz Node.js Backend Development Bootcamp z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Node.js Backend Development Bootcamp?

Nie wymagamy żadnego doświadczenia. Node.js Backend Development Bootcamp w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Obsługa danych żądania: body, query i params”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Node.js Backend Development Bootcamp?

Tak. Każda lekcja Node.js Backend Development Bootcamp zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Podstawy frameworka Express.js
  2. Routing i middleware w Express
  3. Projektowanie endpointów RESTful API
  4. Obsługa danych żądania: body, query i params
← Powrót do Node.js Backend Development Bootcamp