Bird
Raised Fist0
Expressframework~20 mins

Express application structure - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express Structure 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 handler code. What will the client receive when accessing /hello?
Express
const express = require('express');
const app = express();

app.get('/hello', (req, res) => {
  res.send('Hello, world!');
});

app.listen(3000);
AThe client receives a JSON object { message: 'Hello, world!' }
BThe client receives the text 'Hello, world!'
CThe client receives an empty response
DThe server crashes with an error
Attempts:
2 left
💡 Hint
Look at what the res.send method sends back.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets up a middleware in Express?
You want to add a middleware that logs every request method and URL. Which code snippet correctly does this?
Aapp.use((req, res) => { console.log(req.method, req.url); });
Bapp.addMiddleware((req, res, next) => { console.log(req.method, req.url); next(); });
Capp.middleware((req, res, next) => { console.log(req.method, req.url); next(); });
Dapp.use((req, res, next) => { console.log(req.method, req.url); next(); });
Attempts:
2 left
💡 Hint
Express uses a specific method to add middleware functions.
🔧 Debug
advanced
2:00remaining
Why does this Express app crash on startup?
Examine the code below. Why does the server crash when you run it?
Express
const express = require('express');
const app = express();

app.get('/test', (req, res) => {
  res.send('Test route');
});

app.listen(3000);
console.log('Server started');
ATypeError because res.send is called incorrectly
BNo error; server starts normally
CSyntaxError due to missing closing parenthesis in app.listen call
DReferenceError because express is not defined
Attempts:
2 left
💡 Hint
Check the parentheses in the app.listen line.
❓ state_output
advanced
2:00remaining
What is the value of count after these requests?
Given this Express app, what is the value of count after three GET requests to /count?
Express
const express = require('express');
const app = express();
let count = 0;

app.get('/count', (req, res) => {
  count += 1;
  res.send(`Count is ${count}`);
});

app.listen(3000);
A3
B1
C0
DUndefined
Attempts:
2 left
💡 Hint
Each request increases count by 1.
🧠 Conceptual
expert
2:00remaining
Which statement best describes Express app structure?
Which of the following best describes the typical structure of an Express application?
AAn Express app uses modular routing and middleware, separating concerns into different files.
BExpress apps require a database connection in the main file to run.
CExpress apps do not support middleware functions.
DAn Express app is a single file with all routes and middleware defined inline.
Attempts:
2 left
💡 Hint
Think about how to keep code organized and maintainable.

Practice

(1/5)
1. What is the main purpose of the app.js file in an Express application?
easy
A. To set up the Express server, middleware, and routes
B. To store database connection details only
C. To write frontend HTML code
D. To handle user authentication exclusively

Solution

  1. Step 1: Understand the role of app.js

    The app.js file is the main entry point where the Express server is created and configured.
  2. Step 2: Identify what app.js typically contains

    It usually sets up middleware, routes, and starts the server listening on a port.
  3. Final Answer:

    To set up the Express server, middleware, and routes -> Option A
  4. Quick Check:

    Main server setup = C [OK]
Hint: Remember: app.js is the main server setup file [OK]
Common Mistakes:
  • Confusing app.js with frontend files
  • Thinking app.js only handles database
  • Assuming app.js is for authentication only
2. Which of the following is the correct way to import the Express module and create an app instance in app.js?
easy
A. import app from 'express'; const express = app();
B. import express from 'express'; const app = express();
C. const app = require('express');
D. const express = require('express'); const app = express();

Solution

  1. Step 1: Recall common Express import syntax

    Express is commonly imported using CommonJS syntax: const express = require('express');
  2. Step 2: Create app instance correctly

    The app instance is created by calling express(), so const app = express(); is correct.
  3. Final Answer:

    const express = require('express'); const app = express(); -> Option D
  4. Quick Check:

    CommonJS import + app creation = A [OK]
Hint: Use require() and call express() to create app [OK]
Common Mistakes:
  • Mixing ES module import with CommonJS require
  • Calling require('express') directly without variable
  • Incorrect import syntax like importing app instead of express
3. Given this Express app structure snippet in app.js:
const express = require('express');
const app = express();

app.use('/api', require('./routes/api'));

app.listen(3000);
What happens when a user visits http://localhost:3000/api/users?
medium
A. The server returns a 404 error because no route is defined
B. The request is handled by the router defined in ./routes/api
C. The server crashes due to missing route handler
D. The request is handled by the root route /

Solution

  1. Step 1: Understand app.use('/api', ...) behavior

    This mounts the router from ./routes/api at the path /api.
  2. Step 2: Analyze the request URL /api/users

    The request matches the /api prefix, so it is forwarded to the router in ./routes/api which should handle /users.
  3. Final Answer:

    The request is handled by the router defined in ./routes/api -> Option B
  4. Quick Check:

    Mounted router handles prefixed routes = B [OK]
Hint: Routes under '/api' go to './routes/api' router [OK]
Common Mistakes:
  • Assuming 404 because route not in app.js
  • Thinking server crashes without explicit route in app.js
  • Confusing root route with mounted router path
4. Consider this Express app snippet:
const express = require('express');
const app = express();

app.use((req, res) => {
  res.send('');
});

app.get('/home', (req, res) => {
  res.send('Welcome Home');
});

app.listen(3000);
Why does visiting http://localhost:3000/home return a blank page?
medium
A. Because the route handler is not properly defined or not reached
B. Because the server is not listening on port 3000
C. Because the middleware to parse requests is missing
D. Because res.send is missing a return statement

Solution

  1. Step 1: Check if route handler is correct

    The route /home is defined with app.get and sends a response.
  2. Step 2: Consider possible reasons for blank response

    If the page is blank, it usually means the route handler is not reached or overridden by other middleware or routes.
  3. Final Answer:

    Because the route handler is not properly defined or not reached -> Option A
  4. Quick Check:

    Blank page means route not reached = D [OK]
Hint: Blank page usually means route handler not reached [OK]
Common Mistakes:
  • Thinking res.send needs return statement
  • Assuming server not listening without error
  • Confusing missing middleware with route handling
5. You want to organize your Express app by separating routes into different files. Which structure correctly shows how to use a separate users.js route file inside a routes folder and mount it in app.js?
hard
A. // In routes/users.js const router = require('express.Router')(); router.get('/', (req, res) => res.send('Users list')); module.exports = router; // In app.js app.use('/users', require('./routes/users'));
B. // In routes/users.js module.exports = (req, res) => res.send('Users list'); // In app.js app.use('/users', require('./routes/users'));
C. // In routes/users.js const express = require('express'); const router = express.Router(); router.get('/', (req, res) => res.send('Users list')); module.exports = router; // In app.js const usersRouter = require('./routes/users'); app.use('/users', usersRouter);
D. // In routes/users.js const express = require('express'); const router = express.Router; router.get('/', (req, res) => res.send('Users list')); module.exports = router; // In app.js const usersRouter = require('./routes/users'); app.use('/users', usersRouter);

Solution

  1. Step 1: Check correct router creation in users.js

    The router should be created with express.Router(), not express() or other calls.
  2. Step 2: Verify correct export and mounting in app.js

    The router is exported with module.exports and mounted with app.use('/users', usersRouter);.
  3. Final Answer:

    Using express.Router() in the route file and proper mounting -> Option C
  4. Quick Check:

    Use express.Router() and app.use() = A [OK]
Hint: Use express.Router() in route files and mount with app.use() [OK]
Common Mistakes:
  • Using express() instead of express.Router() for router
  • Exporting a function instead of a router object
  • Not mounting the router with app.use() properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard