Lesson 3 / 25
Your First Server
Start a working Express server with one route and app.listen in under ten lines.
Hello, World!
Create an app, define one route, start listening. Save as server.js and run with node server.js.
import express from 'express';
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Output:
Server running on http://localhost:3000
app, listen, and the request handler
express() creates the app instance. app.get(path, handler) registers a route. app.listen(port) starts the HTTP server and keeps the process alive.
Quick check: What does `app.listen(3000)` do?
- Registers a new route
- Parses incoming JSON
- Starts the server and binds it to port 3000
- Creates a new route
Answer
Starts the server and binds it to port 3000 — `listen` binds and starts the underlying HTTP server on the given port.