Client applications transmit data to backend APIs through three primary channels: Route Parameters (identifying specific entities in the URL path), Query Strings (filtering, pagination, and sorting parameters), and the Request Body (structured JSON payloads).
Route parameters are named URL segments used to capture values at specified positions in the URL path. They are defined using a colon : prefix:
1 import express from 'express' ;
2 const app = express ( ) ;
3
4 // Single parameter: /api/users/42
5 app . get ( '/api/users/:id' , ( req , res ) => {
6 const userId = req . params . id ; // "42" (string type)
7 res . json ( { id : userId } ) ;
8 } ) ;
9
10 // Multiple parameters: /api/courses/react-101/reviews/99
11 app . get ( '/api/courses/:courseSlug/reviews/:reviewId' , ( req , res ) => {
12 const { courseSlug , reviewId } = req . params ;
13 res . json ( { courseSlug , reviewId } ) ;
14 } ) ;
Note: All values in req.params are initially strings. Convert them with Number(req.params.id) or parseInt() when querying numeric database IDs.
Query strings follow the ? character in a URL and are separated by &. They are commonly used for filtering, pagination, and searching:
1 GET /api/products?category=electronics&minPrice=100&sort=asc&page=2
Express parses query strings automatically into the req.query object:
1 app . get ( '/api/products' , ( req , res ) => {
2 const { category , minPrice , sort , page = 1 , limit = 20 } = req . query ;
3
4 console . log ( 'Category filter:' , category ) ;
5 console . log ( 'Minimum price:' , minPrice ) ;
6 console . log ( 'Page number:' , page ) ;
7
8 res . json ( {
9 filters : { category , minPrice : Number ( minPrice ) } ,
10 pagination : { page : Number ( page ) , limit : Number ( limit ) }
11 } ) ;
12 } ) ;
3. Request Body (req.body) The request body carries large, structured payloads (JSON, form data) typically in POST, PUT, or PATCH requests. In Express, you MUST register body-parsing middleware before accessing req.body:
1 // Parse application/json
2 app . use ( express . json ( { limit : '1mb' } ) ) ;
3
4 // Parse application/x-www-form-urlencoded (HTML form submissions)
5 app . use ( express . urlencoded ( { extended : true } ) ) ;
6
7 app . post ( '/api/orders' , ( req , res ) => {
8 const { items , shippingAddress , paymentToken } = req . body ;
9
10 if ( ! items || items . length === 0 ) {
11 return res . status ( 400 ) . json ( { error : 'Order must contain at least one item' } ) ;
12 }
13
14 res . status ( 201 ) . json ( {
15 orderId : 'ord_12345' ,
16 status : 'CONFIRMED' ,
17 totalItems : items . length
18 } ) ;
19 } ) ;
Interactive Quiz • Knowledge Check
Multiple Choice Questions Given the route definition app.get('/articles/:category/:id'), what will req.params contain when a client accesses /articles/tech/55?
A { category: 'articles', id: 'tech' }
B { category: 'tech', id: '55' }
C ['tech', 55]
D { query: '55' }
In an incoming URL /api/search?q=nodejs&sort=desc, how do you access the search term nodejs in an Express handler?
A req.params.q
B req.query.q
C req.body.q
D req.headers.q
What will req.body evaluate to if a client sends a JSON payload to an Express app that has NOT registered app.use(express.json())?
A It throws a syntax error on the client.
B It will be undefined.
C It will automatically convert to an empty array.
D It prints a warning to the console and exits.
What is the data type of req.params.id in the route /users/:id when the user requests /users/100?
A Number
B String
C BigInt
D Boolean
Why should you configure { limit: '1mb' } in express.json({ limit: '1mb' })?
A To speed up the internet connection.
B To protect the server against Denial of Service (DoS) attacks by rejecting excessively large JSON payloads.
C Because Express cannot support files larger than 1MB.
D To comply with JavaScript array length limits.