-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (46 loc) · 1.22 KB
/
Copy pathapp.js
File metadata and controls
57 lines (46 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { loxpress } from './loxpress.js'
const app = loxpress()
// Logging middleware
app.use((req, res, next) => {
// console.log(`${req.method} ${req.path}`)
next()
})
// Root route
app.get('/', (req, res) => {
res.json({ message: 'Welcome to Loxpress!' })
})
// Route params + query string
app.get('/users/:id', (req, res) => {
res.json({ id: req.params.id, query: req.query })
})
// Search with query params
app.get('/search', (req, res) => {
res.json({ results: [], query: req.query })
})
// POST with JSON body
app.post('/users', (req, res) => {
res.status(201).json({ created: req.body })
})
// PUT update
app.put('/users/:id', (req, res) => {
res.json({ updated: req.params.id, data: req.body })
})
// DELETE
app.delete('/users/:id', (req, res) => {
res.status(204).end()
})
// Path-scoped middleware
app.use('/api', (req, res, next) => {
res.set('X-Api-Version', '1.0')
next()
})
app.get('/api/status', (req, res) => {
res.json({ status: 'ok' })
})
// Export main so lo's global_main calls it after module import completes.
// app.listen() blocks forever (drives its own event loop).
export function main () {
app.listen(3000, () => {
console.log('Loxpress server running on http://localhost:3000')
})
}