import express from 'express'; const app = express(); app.use(express.json()); app.get('/api/users/:id', (req, res) => { res.json({ id: req.params.id, name: '张三' }); }); app.post('/api/users', (req, res) => { res.status(201).json({ created: req.body }); }); // 统一错误处理:四个参数才会被识别为错误中间件 app.use((err, req, res, next) => { console.error(err); res.status(500).json({ error: 'internal error' }); }); app.listen(3000, () => console.log('http://localhost:3000'));