Menu

I Built a Contact Form API to Understand Express Request & Response Objects
πŸ–ΌοΈ

I Built a Contact Form API to Understand Express Request & Response Objects

DEV Community: expressΒ·ChinwubaΒ·6 months ago
#pzYsIMmn
#dev#json#name#query#found
Reading 0:00
15s threshold

I learn best by building. So instead of reading another article about Express, I built a small Contact Form API with two endpoints and forced myself to use req.body , req.params , req.query , and res.json() in a real context.

Here's what I built and what I actually learned.

The Setup

Two endpoints:

POST /contact β€” accepts name, email, message. Saves to an array. Returns the created entry.

GET /contacts β€” returns all submissions, with optional name filtering.

No database. Just an in-memory array. Simple enough to stay focused, real enough to surface actual problems.

req.body β€” The Envelope

When a client sends a POST request with JSON data, that data lives in req.body . But Express doesn't read it automatically β€” you have to tell it to:

app.use(express.json())

Without that line, req.body is undefined. I think of it as opening the envelope before reading the letter.

req.query β€” The Optional Extras

For the GET route, I added filtering by name.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.