SpaceNode
While SpaceNode's auto-discovery system handles most routing, sometimes you need to register routes manually — for health checks, one-off endpoints, or dynamic route building.
app.setRoute(method, path, handler, pipeNames?)
| Param | Type | Description |
|---|---|---|
method | string | HTTP method ('GET', 'POST', etc.) |
path | string | Route path ('/health', '/users/:id') |
handler | Function | Handler function (same signature as controller handlers) |
pipeNames | string[] | Optional pipe names (['auth', 'cors']) |
const app = await createApp()
app.setRoute('GET', '/health', ({ send }) => {
send({ status: 'ok', uptime: process.uptime() })
})
app.listen(3000)
app.setRoute('GET', '/admin/stats', ({ send }) => {
send({ users: 42, orders: 100 })
}, ['auth', 'role:admin'])
app.setRoute('POST', '/webhook', ({ body, send, emit }) => {
console.log('Webhook received:', body)
emit('webhook:received', body)
send(200, { received: true })
})
app
.setRoute('GET', '/health', ({ send }) => send({ ok: true }))
.setRoute('GET', '/version', ({ send }) => send({ v: '1.0' }))
.setRoute('POST', '/echo', ({ body, send }) => send(body))
.listen(3000)
setRoute() routes work alongside auto-discovered module routes. They're registered in the same trie router and have the same pipeline execution.