0
0
Expressframework~20 mins

Creating documents in Express - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Document Creator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express route handler?
Consider this Express route that creates a new document in a MongoDB collection using Mongoose. What will the client receive when this route is called successfully?
Express
app.post('/items', async (req, res) => {
  const item = new Item({ name: req.body.name });
  await item.save();
  res.status(201).json({ id: item._id, name: item.name });
});
A{"id":"<some_id>","name":"<name_from_request>"} with HTTP status 201
BA plain text message 'Item created' with HTTP status 201
CA JSON with the entire item document including __v and timestamps
DAn empty response with HTTP status 200
Attempts:
2 left
💡 Hint
Look at what is sent in the response and the status code.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a new document and handles errors in Express?
Identify the correct Express route code that creates a new document and sends a 500 status if saving fails.
A
app.post('/users', (req, res) =&gt; {
  const user = new User(req.body);
  user.save();
  res.status(201).json(user);
});
B
app.post('/users', async (req, res) =&gt; {
  try {
    const user = new User(req.body);
    await user.save();
    res.status(201).json(user);
  } catch (e) {
    res.status(500).send('Error');
  }
});
C
app.post('/users', async (req, res) =&gt; {
  const user = new User(req.body);
  await user.save();
  res.json(user);
});
D
app.post('/users', async (req, res) =&gt; {
  try {
    const user = new User(req.body);
    user.save();
    res.status(201).json(user);
  } catch (e) {
    res.status(500).send('Error');
  }
});
Attempts:
2 left
💡 Hint
Look for proper async/await usage and error handling.
🔧 Debug
advanced
2:00remaining
Why does this Express route not save the document?
This route is supposed to create and save a new document but the database remains unchanged. What is the cause?
Express
app.post('/posts', (req, res) => {
  const post = new Post(req.body);
  post.save();
  res.status(201).json({ message: 'Post created' });
});
AThe HTTP method should be GET to create documents.
BThe Post model is not imported, causing a ReferenceError.
CThe route is missing res.end(), so the response never finishes.
Dpost.save() is asynchronous but not awaited, so errors are not caught and save may not complete before response.
Attempts:
2 left
💡 Hint
Think about asynchronous operations and their completion.
state_output
advanced
2:00remaining
What is the value of 'result' after this Express route runs?
Given this code snippet, what will be the value of the variable 'result' after the route is called?
Express
let result = null;
app.post('/data', async (req, res) => {
  const doc = new Data(req.body);
  result = await doc.save();
  res.status(201).json(result);
});
AAn object representing the saved document including _id and other fields
Bnull, because 'result' is not updated outside the async function
Cundefined, since 'result' is declared but never assigned
DA promise object representing the save operation
Attempts:
2 left
💡 Hint
Consider how async/await updates variables in the same scope.
🧠 Conceptual
expert
2:00remaining
Which option best describes the role of middleware when creating documents in Express?
When building an Express app that creates documents, what is the primary purpose of middleware functions in the request handling chain?
ATo send the final response to the client after document creation
BTo directly save documents to the database without route handlers
CTo process and validate incoming data before the document is created
DTo replace the need for database models and schemas
Attempts:
2 left
💡 Hint
Think about what middleware does before the main route logic.