docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
A simple Koa.js application that implements route handlers with regex-based path constraints to restrict access to specific user types.
Implement a Koa.js application with route handlers that use path parameter constraints to create specialized routes for different user categories. Your implementation should handle the following scenarios:
Admin Routes: Create a GET route at /user/:username that only matches when the username is exactly "admin". When matched, the handler should set ctx.body to { role: "administrator", username: "admin" }.
Power User Routes: Create a GET route at /user/:username that only matches usernames starting with "power" (e.g., "power1", "power2", "poweruser"). When matched, the handler should set ctx.body to { role: "power_user", username: <matched_username> }.
Numeric ID Routes: Create a GET route at /id/:userId that only matches numeric user IDs (one or more digits). When matched, the handler should set ctx.body to { userId: <matched_id>, type: "numeric" }.
Fallback Route: Create a final GET route at /user/:username that matches any username not caught by the previous constraints. This handler should set ctx.body to { role: "regular_user", username: <matched_username> }.
/user/admin returns { role: "administrator", username: "admin" } @test/user/power1 returns { role: "power_user", username: "power1" } @test/user/poweruser returns { role: "power_user", username: "poweruser" } @test/id/12345 returns { userId: "12345", type: "numeric" } @test/user/alice returns { role: "regular_user", username: "alice" } @test/**
* Creates and configures a Koa application with regex-constrained routes.
* @returns {import('koa')} Configured Koa application instance
*/
function createApp() {
// Implementation here
}
module.exports = { createApp };Provides the web application framework.
Provides routing middleware with path parameter constraints.