Pages
Contents
@xpulse/router β Component Spec
Status: CONCEPT Β· Created March 2026 Parent:
GLOBAL_concept-ecosystem.md,GLOBAL_concept-web.md
Overview
URL matching and route management for the xPulse ecosystem.
One responsibility: match a URL to a handler β nothing more.
Has no knowledge of controller logic, templates, or the HTTP server directly.
Accepts routes (from @xpulse/controller or manually) and
registers them with @xpulse/http.
Dependencies
| @xpulse/router |
| βββ @xpulse/http β registers matched handlers with the server |
| βββ @xpulse/event β emit router:registered |
API
Initialising
| import router from '@xpulse/router'; |
| await router.init(); |
Registers all known routes with @xpulse/http. Called by @xpulse/app
after @xpulse/controller β once all routes are known.
Registering a route
| router.register('GET', '/', handler); |
| router.register('GET', '/info', handler); |
| router.register('GET', '/info/:page?', handler); |
| router.register('POST', '/contact', handler); |
Called internally by @xpulse/controller β but can also be used manually
for routes without a controller.
404 Handler
| router.notFound((req, res) => { |
| res.status(404).send('<h1>Seite nicht gefunden</h1>'); |
| }); |
Without an explicit notFound handler the router returns a plain
404 Not Found plain-text response.
URL Pattern Matching
Supported patterns analogous to @xpulse/http:
| / β exact match |
| /info β exact match |
| /info/:page β required parameter |
| /info/:page? β optional parameter |
| /tool/:tool/:page? β multiple parameters, last one optional |
| // Example: /tool/chat/guide |
| router.register('GET', '/tool/:tool/:page?', handler); |
| // β req.params = { tool: 'chat', page: 'guide' } |
| // Example: /tool/chat |
| // β req.params = { tool: 'chat', page: undefined } |
Emitted Events
| Event | Payload | When |
|---|---|---|
router:registered |
{ method, path } |
when a route is registered |
router:matched |
{ method, path, params } |
when a route is matched |
router:not-found |
{ method, path } |
when no route matches |
| import event from '@xpulse/event'; |
| event.on('router:not-found', ({ method, path }) => { |
| // automatically logged by @xpulse/logger |
| }); |
Interaction with @xpulse/controller
@xpulse/controller calls router.register() for every discovered
controller method β the router does not need to know about controllers:
| @xpulse/controller β router.register('GET', '/info', InfoController@index) |
| @xpulse/controller β router.register('GET', '/info/bla', InfoController@bla) |
| @xpulse/router β http.route('GET', '/info', handler) |
| @xpulse/router β http.route('GET', '/info/bla', handler) |
Debug Integration
When @xpulse/debug is installed as a devDependency, @xpulse/router
automatically provides a DataCollector. @xpulse/debug discovers it via
node_modules/@xpulse/router/src/datacollectors/ β no configuration needed.
| "optionalDependencies": { |
| "@xpulse/debug": "^1.0.0" |
| } |
The RouterCollector (name: 'router', icon: 'π') shows in the Web Profiler:
- Matched Route (kv): Method, Path, Name, Params (as JSON if present)
- Registered Routes (table): all routes known via
router.register()with Method, Path, Name
No toolbar badge (no quantifiable value per request).
Package Structure
| @xpulse/router/ |
| src/ |
| index.js β default export: router object |
| matcher.js β URL pattern matching logic |
| registry.js β route management (register, notFound) |
| test/ |
| matcher.test.js |
| registry.test.js |
| docs/ |
| index.md |
| api.md |
| _meta.json |
| Dockerfile |
| Makefile |
| README.md |
| package.json |
| xpulse.json |