Lesson 3 / 23
Your First Server
A running Express server 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?
- Starts the server and binds it to port 3000
- Registers a new route
- Parses incoming JSON
Answer
Starts the server and binds it to port 3000 — `listen` binds and starts the underlying HTTP server on the given port.