📖 Scenario: You want to create a simple web server that can respond to visitors with a friendly message. This server will listen for requests and send back a greeting.
🎯 Goal: Build a basic Express server that listens on port 3000 and responds with "Hello, Express!" when someone visits the home page.
📋 What You'll Learn
Create an Express app instance
Set a port number variable
Add a route for the home page that sends a greeting
Start the server listening on the port
💡 Why This Matters
🌍 Real World
Basic Express servers are the foundation for many web applications and APIs.
💼 Career
Understanding how to set up and run an Express server is essential for backend web development roles.
Progress0 / 4 steps
1
Set up Express app
Write const express = require('express') to import Express, then create an app instance with const app = express().
Express
Hint
Use require('express') to import Express and then call it as a function to create the app.
2
Set the port number
Create a variable called port and set it to 3000.
Express
Hint
Use const port = 3000 to set the port number.
3
Add a home page route
Use app.get('/', (req, res) => { ... }) to add a route for the home page that sends the text 'Hello, Express!' with res.send().
Express
Hint
The app.get method defines a route. Use res.send to send the response.
4
Start the server
Call app.listen(port, () => { ... }) to start the server listening on the port. Inside the callback, use console.log to show `Server running on port ${port}`.
Express
Hint
Use app.listen with the port and a callback that logs the server start message.
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
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.
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.
Final Answer:
To start the server and listen for incoming requests -> Option B
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
Step 1: Identify modern import syntax
In modern Node.js with ES modules, the correct syntax is import express from 'express';.
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.
Final Answer:
import express from 'express'; -> Option D
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?
D. import express from 'express';
const app = express();
app.get('/data', (req, res) => {
res.json('Hello JSON');
});
app.listen(8080);
Solution
Step 1: Check route method and path
The question asks for a GET route at '/data', so app.get('/data', ...) is correct.
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.
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.
Final Answer:
The snippet with app.get('/data'), res.json({ message: 'Hello JSON' }), app.listen(8080) -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.