最小可运行版本:
npm init -y
npm install expressconst express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('老城区主干道系统已通车');
});
app.listen(PORT, () => {
console.log(`道路调度中心运行在 http://localhost:${PORT}`);
});这个例子里发生了什么?
app = express()
↓
创建一整套道路调度系统
app.get('/', handler)
↓
登记:如果有 GET 车辆来到 "/" 路口,就交给这个窗口处理
app.listen(3000)
↓
开放城门,开始收车请求流转图:
浏览器访问 /
│
▼
Express 收到 GET /
│
▼
匹配到 app.get('/')
│
▼
执行处理函数 (req, res)
│
▼
res.send(...)
│
▼
响应发回客户端