()
| 114 | } |
| 115 | |
| 116 | setupRoutes() { |
| 117 | // Reload endpoint (for testing) |
| 118 | this.app.post('/_reload', (req, res) => { |
| 119 | this.reloadData() |
| 120 | res.json({ message: 'Data reloaded', data: this.data }) |
| 121 | }) |
| 122 | |
| 123 | // Headers endpoint (for header testing) |
| 124 | this.app.get('/headers', (req, res) => { |
| 125 | res.json(req.headers) |
| 126 | }) |
| 127 | |
| 128 | this.app.post('/headers', (req, res) => { |
| 129 | res.json(req.headers) |
| 130 | }) |
| 131 | |
| 132 | // User endpoints |
| 133 | this.app.get('/user', (req, res) => { |
| 134 | console.log(`[GET /user] Serving user data: ${JSON.stringify(this.data.user)}`) |
| 135 | res.json(this.data.user) |
| 136 | }) |
| 137 | |
| 138 | this.app.post('/user', (req, res) => { |
| 139 | this.data.user = { ...this.data.user, ...req.body } |
| 140 | this.saveData() |
| 141 | res.status(201).json(this.data.user) |
| 142 | }) |
| 143 | |
| 144 | this.app.patch('/user', (req, res) => { |
| 145 | this.data.user = { ...this.data.user, ...req.body } |
| 146 | this.saveData() |
| 147 | res.json(this.data.user) |
| 148 | }) |
| 149 | |
| 150 | this.app.put('/user', (req, res) => { |
| 151 | this.data.user = req.body |
| 152 | this.saveData() |
| 153 | res.json(this.data.user) |
| 154 | }) |
| 155 | |
| 156 | // Posts endpoints |
| 157 | this.app.get('/posts', (req, res) => { |
| 158 | res.json(this.data.posts) |
| 159 | }) |
| 160 | |
| 161 | this.app.get('/posts/:id', (req, res) => { |
| 162 | const id = parseInt(req.params.id) |
| 163 | const post = this.data.posts.find(p => p.id === id) |
| 164 | |
| 165 | if (!post) { |
| 166 | // Return empty object instead of 404 for json-server compatibility |
| 167 | return res.json({}) |
| 168 | } |
| 169 | |
| 170 | res.json(post) |
| 171 | }) |
| 172 | |
| 173 | this.app.post('/posts', (req, res) => { |
no test coverage detected