APIs with Express · Chapter 7 of 29
Built-in middleware
By the end of this chapter you can read any request body (JSON, a form, plain text or raw bytes) and answer in JSON when a body is broken or too big. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Middleware
- 6 tasks
- Express
- In the all labs plan
The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.
Why you need this
Ravi forwards three complaints. The Tapri app posts orders as JSON and gets Send the order as JSON back. A partner café posts an HTML form and gets the same. The kitchen wants to send notes as plain text. And the UPI provider will call Tapri when a payment lands, if Tapri checks that each call really came from them.
Express does not read request bodies on its own. A body arrives as a stream of bytes, and until something decodes it, req.body is undefined. Without help, every route would collect the chunks, guess the format and catch the broken ones itself.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
POST /orders: read JSON orders
In createApp, register a JSON parser for the whole app, so the POST /orders route that is already there can read a JSON body { item, qty }. A body sent as application/json, with or without ; charset=utf-8, answers 201 with the priced order, ids counting from 1: { item: 'masala-chai', qty: 2 } gives { id: 1, item: 'masala-chai', qty: 2, total: 80 }.
A request with no body at all still answers 400 with { error: 'Send the order as JSON' }, which the route already sends when req.body is not an object.
- Task 2
Error handler: 400 for broken JSON, 413 for big orders
Cap the JSON parser at 1kb, and add an error handler after the routes that answers the parsers' errors as JSON:
Broken JSON, such as {item: masala-chai, answers 400 with { error: 'That is not valid JSON' } and a JSON Content-Type. So does a bare string such as "masala-chai": only an object or an array may sit at the top.
A JSON body over 1kb (an order with a 1500-character note) answers 413 with { error: 'Too large for Tapri' }. An order with a 900-character note still fits and answers 201.
- Task 3
POST /orders/form: partner forms with a nested customer
In createApp, register a form parser for the whole app, so POST /orders/form (already there) reads partner forms sent as application/x-www-form-urlencoded. Fields named customer[name] and customer[table] must become a nested object: item=cutting-chai&qty=3&customer[name]=Asha&customer[table]=4 answers 201 with { id: 1, item: 'cutting-chai', qty: 3, total: 75, customer: { name: 'Asha', table: '4' } }.
A form over 1kb answers 413.
- Task 4
POST /kitchen/notes: plain text on one route
Write POST /kitchen/notes, which takes the note as plain text (text/plain), at most 200 bytes. The text parser goes on this route only: POST /orders sent as text/plain still answers 400 with { error: 'Send the order as JSON' }.
A note is trimmed, kept in notes and answered 201 with { id, note }, ids counting from 1: ' Less sugar for table 4 ' gives { id: 1, note: 'Less sugar for table 4' }.
A body that is not text, such as JSON, answers 415 with { error: 'Send the note as plain text' }. A note that is only spaces answers 400 with { error: 'Write something for the kitchen' }. A note over 200 bytes answers 413.
- Task 5
POST /orders: the partner type application/vnd.tapri+json
POST /orders must also read bodies whose Content-Type is application/vnd.tapri+json, exactly like application/json: { item: 'cutting-chai', qty: 4 } answers 201 with { id: 1, item: 'cutting-chai', qty: 4, total: 100 }. A broken body of that type answers 400.
Plain application/json must keep working.
- Task 6 · interview question
Interview: POST /webhooks/upi, the signature on the raw body
Finish POST /webhooks/upi. The UPI provider sends JSON (application/json) and puts a signature in the x-upi-signature header: the hex HMAC-SHA256 of the exact body bytes, made with webhookSecret (createApp({ webhookSecret })).
When the signature matches, parse the body, keep the payment in payments and answer 200 with { received: true, orderId } (orderId from the body: 7 for {"orderId": 7, ...}).
Every other case answers 401 with { error: 'Bad signature' }: a signature made with another secret, one made over the body parsed and stringified again (the provider's bytes have extra spaces), a short signature such as abc123, and no header at all. None of them may crash the route.
What you will learn
- express.json and express.urlencoded: parsers that read JSON and form bodies into req.body.
- express.text and express.raw: a string or the exact bytes, often for one route only.
- The type and limit options: which bodies a parser reads, and how big they may be.
- Parser errors: answering a broken or oversized body in JSON.
More in Middleware
Build "Built-in middleware" yourself
Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.