Bird
Raised Fist0
Expressframework~5 mins

Listening on a port in Express

Choose your learning style10 modes available
Introduction
Listening on a port lets your Express app accept requests from users or other programs. It opens a door for communication.
When you want your web app to be reachable on the internet or local network.
When testing your Express server locally to see how it responds.
When deploying your app so it can handle real user traffic.
When running multiple apps on different ports on the same machine.
When you want to specify a custom port instead of the default.
Syntax
Express
const express = require('express');
const app = express();

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});
The listen method starts the server and waits for connections on the given port.
The callback function runs once the server is ready, useful for logging.
Examples
Starts the server on port 3000 without a callback.
Express
app.listen(3000);
Starts the server and logs a message when ready.
Express
app.listen(3000, () => {
  console.log('Server started on port 3000');
});
Uses an environment variable for the port, with 3000 as fallback.
Express
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});
Sample Program
This program creates a simple Express server that responds with 'Hello, world!' when you visit the home page. It listens on port 3000 and logs a message when ready.
Express
const express = require('express');
const app = express();

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

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});
OutputSuccess
Important Notes
Listening on a port is how your server accepts incoming requests.
If the port is already in use, the server will fail to start.
Common mistake: forgetting to call listen, so the server never starts.
Use environment variables for ports in real apps to avoid conflicts.
Summary
Listening on a port opens your app to receive requests.
Use app.listen(port, callback) to start the server.
Always handle the callback to confirm the server started.

Practice

(1/5)
1. What does the app.listen(port, callback) method do in an Express app?
easy
A. Starts the server and listens for incoming requests on the specified port
B. Stops the server from running
C. Sends a response to the client
D. Creates a new route handler

Solution

  1. Step 1: Understand the purpose of app.listen

    This method is used to start the Express server and make it listen on a specific port for incoming requests.
  2. Step 2: Identify what happens when the server listens

    Once listening, the server can accept and respond to client requests on that port.
  3. Final Answer:

    Starts the server and listens for incoming requests on the specified port -> Option A
  4. Quick Check:

    app.listen starts server = B [OK]
Hint: Remember: listen means start and wait for requests [OK]
Common Mistakes:
  • Confusing listen with sending responses
  • Thinking listen stops the server
  • Mixing listen with route creation
2. Which of the following is the correct syntax to start an Express server listening on port 3000 with a callback that logs "Server started"?
easy
A. app.listen(3000).then(() => console.log('Server started'))
B. app.listen(3000, console.log('Server started'))
C. app.listen(3000, () => console.log('Server started'))
D. app.listen(() => console.log('Server started'), 3000)

Solution

  1. Step 1: Recall the correct order of parameters for app.listen

    The first parameter is the port number, the second is a callback function.
  2. Step 2: Check the callback syntax

    The callback should be a function, here an arrow function logging the message.
  3. Final Answer:

    app.listen(3000, () => console.log('Server started')) -> Option C
  4. Quick Check:

    Correct syntax = D [OK]
Hint: Callback must be a function, not a direct call [OK]
Common Mistakes:
  • Passing console.log() directly instead of a function
  • Swapping port and callback parameters
  • Using .then() which is not supported
3. What will be the output when running this Express code snippet?
const express = require('express');
const app = express();
app.listen(4000, () => console.log('Listening on port 4000'));
medium
A. The server starts but does not log anything
B. Syntax error because of missing semicolons
C. Nothing happens because the callback is missing
D. The server starts and logs 'Listening on port 4000' to the console

Solution

  1. Step 1: Analyze the code structure

    The code imports Express, creates an app, and calls app.listen on port 4000 with a callback that logs a message.
  2. Step 2: Understand the callback behavior

    When the server starts listening, the callback runs and logs the message to the console.
  3. Final Answer:

    The server starts and logs 'Listening on port 4000' to the console -> Option D
  4. Quick Check:

    Callback logs message = A [OK]
Hint: Callback runs after server starts listening [OK]
Common Mistakes:
  • Thinking semicolons cause errors in JavaScript
  • Assuming callback is optional and logs nothing
  • Confusing server start with route handling
4. Identify the error in this Express server code:
const express = require('express');
const app = express();
app.listen(() => console.log('Server running'), 5000);
medium
A. The port number should be the first argument to app.listen
B. The callback function should not be an arrow function
C. Missing require statement for express
D. app.listen cannot take a callback function

Solution

  1. Step 1: Check the order of arguments in app.listen

    The first argument must be the port number, but here the callback is first.
  2. Step 2: Confirm correct argument order

    Correct usage is app.listen(port, callback), so the arguments are swapped.
  3. Final Answer:

    The port number should be the first argument to app.listen -> Option A
  4. Quick Check:

    Port first, callback second = A [OK]
Hint: Port number always comes before callback in listen() [OK]
Common Mistakes:
  • Swapping callback and port arguments
  • Thinking arrow functions are invalid callbacks
  • Ignoring argument order importance
5. You want your Express app to listen on a port defined by an environment variable PORT, but default to 8080 if PORT is not set. Which code correctly implements this?
hard
A. app.listen(process.env.PORT ? 8080 : process.env.PORT, () => console.log('Server started'));
B. const port = process.env.PORT || 8080; app.listen(port, () => console.log(`Server running on port ${port}`));
C. const port = 8080 || process.env.PORT; app.listen(port, () => console.log(`Server running on port ${port}`));
D. app.listen(8080, () => console.log('Server running on port 8080'));

Solution

  1. Step 1: Understand environment variable fallback

    Use process.env.PORT || 8080 to use PORT if set, else 8080.
  2. Step 2: Confirm callback logs correct port

    Use a template string to log the actual port used.
  3. Final Answer:

    const port = process.env.PORT || 8080; app.listen(port, () => console.log(`Server running on port ${port}`)); -> Option B
  4. Quick Check:

    Use || for default port = C [OK]
Hint: Use || to set default port if env variable missing [OK]
Common Mistakes:
  • Swapping order in || causing wrong default
  • Using ternary incorrectly for port selection
  • Hardcoding port without fallback
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