Imperative Routes

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()

app.setRoute(method, path, handler, pipeNames?)
ParamTypeDescription
methodstringHTTP method ('GET', 'POST', etc.)
pathstringRoute path ('/health', '/users/:id')
handlerFunctionHandler function (same signature as controller handlers)
pipeNamesstring[]Optional pipe names (['auth', 'cors'])

Examples

Health Check

const app = await createApp()

app.setRoute('GET', '/health', ({ send }) => {
  send({ status: 'ok', uptime: process.uptime() })
})

app.listen(3000)

With Pipes

app.setRoute('GET', '/admin/stats', ({ send }) => {
  send({ users: 42, orders: 100 })
}, ['auth', 'role:admin'])

POST with Body

app.setRoute('POST', '/webhook', ({ body, send, emit }) => {
  console.log('Webhook received:', body)
  emit('webhook:received', body)
  send(200, { received: true })
})

Chaining

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)
TipsetRoute() routes work alongside auto-discovered module routes. They're registered in the same trie router and have the same pipeline execution.