or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-3/

API Namespace Router

Build a Koa.js application that implements flexible path matching for API endpoints under different version namespaces.

Requirements

Your application should implement the following routing behavior:

1. Prefix-based Route Matching

Create routes that match all paths starting with a specific prefix. The routes should match any path that continues beyond the prefix:

  • /api/v1/users should match and capture the remaining path
  • /api/v1/products/123 should match and capture the remaining path
  • /api/v2/orders should match (for v2 routes) and capture the remaining path

2. Path Capture and Response

For each API version route, capture the remaining path after the version prefix and return it in the response. The response body should be a JSON object with:

  • version: The API version (e.g., "v1" or "v2")
  • path: The captured remaining path (e.g., "/users" or "/products/123")

3. Multiple API Versions

Implement separate route handlers for v1 and v2 API versions that process requests to their respective prefixes.

Test Cases

  • Requesting GET /api/v1/users returns JSON {"version":"v1","path":"/users"} @test
  • Requesting GET /api/v1/products/123 returns JSON {"version":"v1","path":"/products/123"} @test
  • Requesting GET /api/v2/orders returns JSON {"version":"v2","path":"/orders"} @test

Implementation

@generates

API

// Create and export a configured Koa application with the routing behavior described above
const app = require('koa')();

// Add your routing logic here

module.exports = app;

Dependencies { .dependencies }

koa { .dependency }

Provides web application framework.

koa-route { .dependency }

Provides route middleware with advanced path matching capabilities.