Core

Routing

Trie-based router with static, dynamic, and wildcard routes.

1 min readUpdated DocumentationEdit this page
On this page4

Route registration

TypeScript
TS
1app.get('/users',      handler)
2app.post('/users',     handler)
3app.put('/users/:id',  handler)
4app.delete('/users/:id', handler)

Dynamic parameters

TypeScript
TS
1app.get('/users/:id', (req, res) => {
2  res.json({ userId: req.params.id })
3})
4
5app.get('/users/:userId/posts/:postId', (req, res) => {
6  res.json({ userId: req.params.userId, postId: req.params.postId })
7})

Wildcard routes

TypeScript
TS
app.get('/files/*', (req, res) => {
  res.json({ path: req.params['*'] })
})

Router modules

TypeScript
TS
1import { Router } from '@axiljs/core'
2
3const users = new Router('/users')
4users.get('/',    listUsers)
5users.get('/:id', getUser)
6users.post('/',   createUser)
7
8app.mount('/api', users)   // GET /api/users/42 → getUser

app.mount(prefix, router) strips the prefix before matching, so a router can also be defined relative to its mount point (new Router() with get('/:id'), mounted at /users).

Tip

Lookup cost in the trie router grows with the number of path segments, not the number of routes. Matching priority is static segment, then :param, then *.

Was this page helpful?

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY