0
0
Expressframework~20 mins

Why database integration matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Integration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use a database with Express?
Why is integrating a database important when building an Express app?
ATo make the Express app run faster by avoiding any data storage.
BTo store and retrieve data persistently across user sessions and server restarts.
CTo replace the need for routing and middleware in Express.
DTo allow Express to run without a server.
Attempts:
2 left
💡 Hint
Think about what happens to data when the server restarts.
component_behavior
intermediate
1:30remaining
What happens when Express app lacks database integration?
Consider an Express app that stores user data only in variables inside the server code. What happens when the server restarts?
AThe server caches data permanently in memory.
BUser data is saved automatically to disk by Express.
CUser data is sent to the client browser for storage.
DAll stored user data is lost because variables reset on restart.
Attempts:
2 left
💡 Hint
Think about where variables live and what happens on server restart.
📝 Syntax
advanced
2:00remaining
Identify the correct way to connect Express with a MongoDB database
Which code snippet correctly connects an Express app to MongoDB using the official MongoDB Node.js driver?
A
import { MongoClient } from 'mongodb';
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('mydb');
B
const MongoClient = require('mongodb').MongoClient;
const client = new MongoClient('mongodb://localhost:27017');
client.connect();
const db = client.db('mydb');
C
import MongoClient from 'mongodb';
const client = new MongoClient('localhost:27017');
await client.connect();
const db = client.database('mydb');
D
const client = new MongoClient('mongodb://localhost:27017');
client.connect();
const db = client.db('mydb');
Attempts:
2 left
💡 Hint
Check for correct import syntax, connection string, and method names.
state_output
advanced
1:30remaining
What is the output when querying a database in Express?
Given this Express route using async/await to fetch users from a database, what will be sent to the client? ```js app.get('/users', async (req, res) => { const users = await db.collection('users').find().toArray(); res.json(users); }); ```
AA string 'users' instead of data.
BAn error because async/await cannot be used in Express routes.
CA JSON array of user objects from the 'users' collection.
DAn empty response because find() returns a cursor, not data.
Attempts:
2 left
💡 Hint
What does toArray() do on a MongoDB cursor?
🔧 Debug
expert
2:30remaining
Why does this Express app fail to save data to the database?
Examine the code below. Why does the new user not get saved to the database? ```js app.post('/add-user', (req, res) => { const user = req.body; db.collection('users').insertOne(user); res.send('User added'); }); ```
ABecause req.body is not parsed automatically without middleware.
BBecause insertOne requires a callback function to work.
CBecause insertOne is asynchronous but not awaited or handled with a callback, so errors are ignored.
DBecause res.send is called before the database connection is established.
Attempts:
2 left
💡 Hint
Does Express parse JSON request bodies automatically?