Learn how to define and organize routes in Asimilation
Routes are the foundation of your Asimilation application. This guide will walk you through creating routes, defining HTTP methods, and organizing your route definitions.
Asimilation automatically normalizes paths for you:
// These are all equivalent:url.addPath('/api/users', handler);url.addPath('api/users', handler); // Leading slash addedurl.addPath('/api//users', handler); // Multiple slashes normalized
Path normalization is handled by the normalizePath() function (see src/helpers/url-regex.ts:38-47).
For larger applications, organize routes using RouteModule. This allows you to create namespaced route groups:
import { url } from '@asimilation/core';// Create a module with a base pathconst apiModule = url.createRouteModule('/api');// All routes in this module are prefixed with /apiapiModule.addPath('/users', (req, res) => { res.sendJson({ users: [] }, 200);}); // Responds to /api/usersapiModule.addPath('/posts', (req, res) => { res.sendJson({ posts: [] }, 200);}); // Responds to /api/posts