API Reference
Routing API Reference
Complete reference for MoroJS routing methods, patterns, and configuration options.
Route Methods
HTTP Methods
app.get(path, config)GET requests
app.post(path, config)POST requests
app.put(path, config)PUT requests
app.delete(path, config)DELETE requests
app.patch(path, config)PATCH requests
Additional Methods
app.route(config)Schema-first routing
app.use(middleware)Global middleware
app.websocket(path, config)WebSocket routes
app.listen(port)Start server
Route Patterns
Pattern Examplestypescript
1// Static routes
2app.get('/users').handler(handler);
3app.get('/api/health').handler(handler);
4
5// Named parameters
6app.get('/users/:id').handler(handler);
7app.get('/users/:userId/posts/:postId').handler(handler);
8
9// Optional parameters
10app.get('/posts/:id?').handler(handler);
11
12// Wildcard routes
13app.get('/files/*').handler(handler);
14app.get('/api/v1/*').handler(handler);
15
16// Regex patterns
17app.get('/users/:id(\d+)').handler(handler); // Only digits
18app.get('/files/:name(.+\.(jpg|png|gif))').handler(handler); // File extensions
19
20// Query parameters with validation
21app.get('/search')
22 .query(z.object({
23 q: z.string(),
24 limit: z.coerce.number().default(10)
25 }))
26 .handler((req) => {
27 // req.query.q, req.query.limit are validated and typed
28 return { results: [] };
29 });Pattern Matching Rules
- Static routes have highest priority
- Named parameters are case-sensitive
- Wildcards match remaining path segments
- Routes are matched in registration order
Route Groups
Basic Route Groupstypescript
1// Group routes with common prefix - Chainable API
2app.group('/api/v1', (group) => {
3 group.get('/users').handler(getUsersHandler); // GET /api/v1/users
4 group.post('/users').handler(createUserHandler); // POST /api/v1/users
5 group.get('/users/:id').handler(getUserHandler); // GET /api/v1/users/:id
6});
7
8// Nested groups
9app.group('/api', (api) => {
10 api.group('/v1', (v1) => {
11 v1.get('/users').handler(handler); // GET /api/v1/users
12 });
13
14 api.group('/v2', (v2) => {
15 v2.get('/users').handler(handler); // GET /api/v2/users
16 });
17});Groups with Middlewaretypescript
1// Apply middleware to entire group
2app.group('/admin', {
3 middleware: [authMiddleware, adminMiddleware]
4}, (group) => {
5 group.get('/users').handler(getAdminUsers);
6 group.post('/users').handler(createAdminUser);
7 group.delete('/users/:id').handler(deleteUser);
8});
9
10// Groups with configuration - all routes inherit
11app.group('/api/v1', {
12 middleware: [corsMiddleware],
13 rateLimit: { requests: 1000, window: 3600000 }
14}, (group) => {
15 group.get('/public').handler(publicHandler);
16 group.get('/private')
17 .use(authMiddleware)
18 .handler(privateHandler);
19});