0
0
Expressframework~30 mins

POST route handling in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
POST route handling
📖 Scenario: You are building a simple Express server that accepts user data through a POST request.This is common when users submit forms on websites.
🎯 Goal: Create an Express server with a POST route at /submit that receives JSON data with a name and age and stores it in a list.
📋 What You'll Learn
Create an Express app
Add middleware to parse JSON request bodies
Create a POST route at /submit
Store received data in an array called users
💡 Why This Matters
🌍 Real World
Handling POST requests is essential for web servers to receive data from users, such as form submissions or API calls.
💼 Career
Backend developers often write POST route handlers to process and store user input securely and efficiently.
Progress0 / 4 steps
1
Set up Express app and users array
Create an Express app by requiring express and calling it. Then create an empty array called users to store user data.
Express
Need a hint?

Use require('express') to import Express.

Call express() to create the app.

Create an empty array users to hold user objects.

2
Add JSON body parsing middleware
Add middleware to the Express app to parse incoming JSON request bodies using app.use(express.json()).
Express
Need a hint?

Use app.use(express.json()) to enable JSON parsing for POST requests.

3
Create POST route at /submit
Create a POST route handler for /submit using app.post. Inside the handler, extract name and age from req.body and push an object with these properties into the users array.
Express
Need a hint?

Use app.post('/submit', (req, res) => { ... }) to create the route.

Extract name and age from req.body.

Push the new user object into users.

Send a 201 status with a confirmation message.

4
Start the server on port 3000
Add code to start the Express server listening on port 3000 using app.listen. Log a message 'Server running on port 3000' when it starts.
Express
Need a hint?

Use app.listen(3000, () => { ... }) to start the server.

Inside the callback, log the message to confirm the server is running.