CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-http-proxy-middleware

The one-liner node.js proxy middleware for connect, express, next.js and more

92

1.24x
Quality

Pending

Does it follow best practices?

Impact

92%

1.24x

Average score across 10 eval scenarios

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

task.mdevals/scenario-7/

Multi-Tenant API Gateway

Build a Node.js API gateway service that routes incoming requests to different backend servers based on tenant information extracted from request headers. The gateway should support multiple routing strategies and handle both synchronous and asynchronous tenant lookups.

Requirements

Your implementation should:

  1. Create an Express server that listens on port 3000
  2. Implement a proxy middleware that routes requests based on tenant identification
  3. Support two routing modes:
    • Static routing: Map known tenant IDs to specific backend URLs
    • Dynamic routing: Perform asynchronous database lookups for unknown tenants
  4. Extract tenant ID from the X-Tenant-ID request header
  5. For known tenants (tenant-a, tenant-b, tenant-c), use predefined backend URLs
  6. For other tenant IDs, simulate an async database lookup to determine the backend
  7. Return a 404 error for requests without a tenant ID or for invalid tenant IDs

Static Tenant Mappings

  • tenant-ahttp://localhost:4001
  • tenant-bhttp://localhost:4002
  • tenant-chttp://localhost:4003

Dynamic Tenant Lookup

For tenant IDs not in the static mapping, simulate a database lookup that:

  • Takes 50ms to complete (simulated async operation)
  • Returns backend URL in format: http://localhost:4004 for valid tenants
  • Returns null for invalid tenant IDs (e.g., tenant IDs that start with "invalid-")

Test Cases

The following test cases should pass:

  • Given a request with header X-Tenant-ID: tenant-a, the proxy routes to http://localhost:4001 @test
  • Given a request with header X-Tenant-ID: tenant-unknown, the proxy performs async lookup and routes to http://localhost:4004 @test
  • Given a request with header X-Tenant-ID: invalid-tenant, the proxy returns 404 status @test
  • Given a request without X-Tenant-ID header, the proxy returns 404 status @test

Implementation

@generates

API

/**
 * Simulates async database lookup for tenant configuration
 * @param {string} tenantId - The tenant identifier
 * @returns {Promise<string|null>} Backend URL or null if tenant is invalid
 */
async function lookupTenantBackend(tenantId) {
  // IMPLEMENTATION HERE
}

/**
 * Creates and configures the Express app with proxy middleware
 * @returns {Express} Configured Express application
 */
function createApp() {
  // IMPLEMENTATION HERE
}

module.exports = { createApp, lookupTenantBackend };

Dependencies { .dependencies }

http-proxy-middleware { .dependency }

Provides HTTP proxy middleware functionality with dynamic routing support.

express { .dependency }

Provides web server framework for handling HTTP requests.

tile.json