リクエストデータの処理:Body、Query、Params
Expressがクライアントから受け取ったデータを処理・解析する方法を学びます。ルートパラメーター、クエリ文字列、JSONボディ、フォーム送信を扱います。
「リクエストデータの処理:Body、Query、Params」はCoddyKit上の無料Node.js Backend Development Bootcampレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNode.js Backend Development Bootcamp学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Node.js Backend Development Bootcampコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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()soreq.bodyisundefined - 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.paramsfor named URL segmentsreq.queryfor the query stringreq.bodyfor JSON (withexpress.json()) and forms (withexpress.urlencoded())req.headers/req.get()for metadata
Always validate and provide defaults — never trust raw client input.
よくある質問
「リクエストデータの処理:Body、Query、Params」レッスンは無料ですか?
はい。「リクエストデータの処理:Body、Query、Params」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Node.js Backend Development Bootcampコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Node.js Backend Development Bootcampコースには全4レッスンが含まれています。
「リクエストデータの処理:Body、Query、Params」で何を学びますか?
Expressがクライアントから受け取ったデータを処理・解析する方法を学びます。ルートパラメーター、クエリ文字列、JSONボディ、フォーム送信を扱います。 ブラウザで直接実行するハンズオンコードでNode.js Backend Development Bootcampを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Node.js Backend Development Bootcampを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNode.js Backend Development Bootcampは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「リクエストデータの処理:Body、Query、Params」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNode.js Backend Development Bootcampレッスンでコードを書いて実行できますか?
はい。すべてのNode.js Backend Development Bootcampレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Express.jsフレームワークの基礎
- Expressのルーティングとミドルウェア
- RESTful APIエンドポイントを設計する
- リクエストデータの処理:Body、Query、Params