📖 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
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
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
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
Hint
Use app.listen(3000, () => { ... }) to start the server.
Inside the callback, log the message to confirm the server is running.
Practice
(1/5)
1. What does the app.post method in Express do?
easy
A. It defines a route that handles HTTP GET requests.
B. It defines a route that handles HTTP POST requests.
C. It starts the Express server.
D. It sends a response to the client automatically.
Solution
Step 1: Understand HTTP methods in Express
Express uses different methods like app.get and app.post to handle different HTTP request types.
Step 2: Identify the purpose of app.post
app.post specifically handles POST requests, which are used to send data to the server.
Final Answer:
It defines a route that handles HTTP POST requests. -> Option B
Quick Check:
POST route = app.post [OK]
Hint: POST routes handle data sent by clients via app.post [OK]
Common Mistakes:
Confusing POST with GET routes
Thinking app.post starts the server
Assuming app.post sends responses automatically
2. Which of the following is the correct syntax to create a POST route in Express for path /submit?
easy
A. app.post('/submit', (req, res) => res.send('Received'));
B. app.get('/submit', (req, res) => { res.send('Received'); });
C. app.post('/submit', (res, req) => { res.send('Received'); });
D. app.post('/submit', (req, res) => { res.send('Received'); });
Solution
Step 1: Check method and path
The method should be app.post and path '/submit'.
Step 2: Verify callback parameters and syntax
The callback function must have parameters (req, res) in that order, and the response should be sent inside the function body.
Final Answer:
app.post('/submit', (req, res) => { res.send('Received'); }); -> Option D
Hint: Always add express.json() to parse JSON POST bodies [OK]
Common Mistakes:
Assuming req.body works without middleware
Swapping req and res parameters
Thinking route path name matters for parsing
5. You want to create a POST route /register that accepts JSON data with username and password. It should respond with { success: true, user: username }. Which code correctly implements this?