Bird
Raised Fist0
Expressframework~20 mins

Creating a basic Express server - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express Server Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing the root URL?
Consider this Express server code. What will the server respond with when you visit http://localhost:3000/ in a browser?
Express
import express from 'express';
const app = express();
app.get('/', (req, res) => {
  res.send('Hello from Express!');
});
app.listen(3000);
AThe browser shows the text: Hello from Express!
BThe server crashes with an error
CThe browser shows a blank page with status 200
DThe browser shows a 404 Not Found error
Attempts:
2 left
💡 Hint
Look at the route defined with app.get and what it sends back.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates an Express server listening on port 8080?
Choose the code snippet that correctly starts an Express server on port 8080.
A
const app = express();
app.listen(8080, () => console.log('Server running on 8080'));
B
const app = express;
app.listen(8080);
C
const app = express();
app.listen(8080); console.log('Server running on 8080');
D
const app = express();
app.start(8080);
Attempts:
2 left
💡 Hint
Remember to call express as a function and use the correct method to start the server.
🔧 Debug
advanced
2:00remaining
Why does this Express server code crash?
This code crashes when run. What is the cause of the error?
Express
import express from 'express';
const app = express();
app.get('/', (req, res) => {
  res.send('Welcome!');
});
app.listen();
Aexpress() must be called with a port number
Bapp.listen() is missing the port number argument
Cres.send() must be called with an object, not a string
Dapp.get() requires a second argument to be a named function
Attempts:
2 left
💡 Hint
Check the app.listen() method usage.
❓ state_output
advanced
2:00remaining
What is the response when accessing '/user/42'?
Given this Express route, what will the server respond with when visiting /user/42?
Express
import express from 'express';
const app = express();
app.get('/user/:id', (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});
app.listen(3000);
A404 Not Found error
BUser ID is undefined
CUser ID is :id
DUser ID is 42
Attempts:
2 left
💡 Hint
Look at how route parameters are accessed in Express.
🧠 Conceptual
expert
2:00remaining
What happens if you call app.listen() twice in the same Express app?
Consider an Express app where app.listen(3000) is called, and later app.listen(4000) is called again. What is the expected behavior?
AThe app stops listening on 3000 and only listens on 4000
BThe app listens on both ports 3000 and 4000 simultaneously
CThe second call throws an error because the app is already listening
DThe app ignores the second call and continues listening on 3000
Attempts:
2 left
💡 Hint
Think about how Node.js servers handle multiple listen calls on the same app instance.

Practice

(1/5)
1. What is the main purpose of app.listen() in an Express server?
easy
A. To define a route for GET requests
B. To start the server and listen for incoming requests
C. To import the Express library
D. To send a response to the client

Solution

  1. Step 1: Understand the role of app.listen()

    This function starts the Express server and makes it listen on a specified port for incoming requests.
  2. Step 2: Compare with other functions

    Functions like app.get() define routes, but do not start the server. Importing Express or sending responses are done elsewhere.
  3. Final Answer:

    To start the server and listen for incoming requests -> Option B
  4. Quick Check:

    app.listen() starts server [OK]
Hint: Remember: listen means server waits for requests [OK]
Common Mistakes:
  • Confusing route definition with server start
  • Thinking app.listen() sends responses
  • Mixing import statements with server functions
2. Which of the following is the correct way to import Express in a modern Node.js project?
easy
A. const express = require('express');
B. load express from 'express';
C. var express = import('express');
D. import express from 'express';

Solution

  1. Step 1: Identify modern import syntax

    In modern Node.js with ES modules, the correct syntax is import express from 'express';.
  2. Step 2: Check other options

    require() is CommonJS style, not modern ES modules. var express = import('express'); is invalid syntax. load is not a valid keyword.
  3. Final Answer:

    import express from 'express'; -> Option D
  4. Quick Check:

    Modern Node.js uses import [OK]
Hint: Use 'import' for modern Node.js, not 'require' [OK]
Common Mistakes:
  • Using require() in ES module projects
  • Trying to use 'load' keyword
  • Incorrect use of dynamic import syntax
3. What will be the output when you visit http://localhost:3000/hello given this Express code?
import express from 'express';
const app = express();
app.get('/hello', (req, res) => {
  res.send('Hello World!');
});
app.listen(3000);
medium
A. Hello World!
B. Cannot GET /hello
C. 404 Not Found error
D. Server error

Solution

  1. Step 1: Analyze the route definition

    The code defines a GET route at '/hello' that sends 'Hello World!' as a response.
  2. Step 2: Understand server behavior on request

    When visiting /hello, the server matches the route and sends the defined response.
  3. Final Answer:

    Hello World! -> Option A
  4. Quick Check:

    Defined route sends 'Hello World!' [OK]
Hint: Check if route matches URL path exactly [OK]
Common Mistakes:
  • Visiting a route not defined causes 404
  • Confusing response text with error messages
  • Forgetting to start server with listen()
4. Identify the error in this Express server code:
import express from 'express';
const app = express();
app.get('/', (req, res) => {
  res.send('Welcome!');
});
app.listen();
medium
A. Missing port number in app.listen()
B. Incorrect route path in app.get()
C. res.send() should be res.sendResponse()
D. Express not imported correctly

Solution

  1. Step 1: Check app.listen() usage

    The app.listen() method requires a port number to start the server. Here, no port is given.
  2. Step 2: Verify other parts

    The route path '/' is valid, res.send() is correct, and Express is imported properly.
  3. Final Answer:

    Missing port number in app.listen() -> Option A
  4. Quick Check:

    app.listen() needs port [OK]
Hint: Always specify a port in app.listen(port) [OK]
Common Mistakes:
  • Omitting port number in app.listen()
  • Using wrong method names for response
  • Incorrect route paths syntax
5. You want to create an Express server that responds with JSON at /data and listens on port 8080. Which code snippet correctly does this?
hard
A. import express from 'express'; const app = express(); app.get('/data', (req, res) => { res.send('Hello JSON'); }); app.listen(3000);
B. import express from 'express'; const app = express(); app.post('/data', (req, res) => { res.send({ message: 'Hello JSON' }); }); app.listen(8080);
C. import express from 'express'; const app = express(); app.get('/data', (req, res) => { res.json({ message: 'Hello JSON' }); }); app.listen(8080);
D. import express from 'express'; const app = express(); app.get('/data', (req, res) => { res.json('Hello JSON'); }); app.listen(8080);

Solution

  1. Step 1: Check route method and path

    The question asks for a GET route at '/data', so app.get('/data', ...) is correct.
  2. Step 2: Verify response and port

    Response should be JSON, so res.json({ message: 'Hello JSON' }) is correct. The server must listen on port 8080.
  3. Step 3: Eliminate alternatives with errors

    Alternatives fail due to using POST instead of GET, wrong port like 3000, res.send('string') instead of res.json(object), or res.json('string') instead of object.
  4. Final Answer:

    The snippet with app.get('/data'), res.json({ message: 'Hello JSON' }), app.listen(8080) -> Option C
  5. Quick Check:

    GET /data + res.json + port 8080 [OK]
Hint: Use app.get + res.json + correct port [OK]
Common Mistakes:
  • Using POST instead of GET for route
  • Sending string instead of JSON object
  • Listening on wrong port number
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