0Pricing
Node.js Backend Development Bootcamp · บทเรียน

การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์

เรียนรู้วิธีที่ Express รับและแยกวิเคราะห์ข้อมูลขาเข้าจากไคลเอนต์ ทั้งพารามิเตอร์เส้นทาง สตริงคิวรี เนื้อหา JSON และข้อมูลจากแบบฟอร์ม

การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์ เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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() 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.

คำถามที่พบบ่อย

บทเรียน “การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์”

เรียนรู้วิธีที่ Express รับและแยกวิเคราะห์ข้อมูลขาเข้าจากไคลเอนต์ ทั้งพารามิเตอร์เส้นทาง สตริงคิวรี เนื้อหา JSON และข้อมูลจากแบบฟอร์ม คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานกรอบงาน Express.js
  2. การกำหนดเส้นทางและมิดเดิลแวร์ใน Express
  3. การออกแบบจุดเชื่อมต่อ RESTful API
  4. การจัดการข้อมูลคำขอ: เนื้อหา คิวรี และพารามิเตอร์
← กลับไปที่ Node.js Backend Development Bootcamp