xPulse
πŸ‡¬πŸ‡§ EN

@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:

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
en/spec.md 2026-03-27