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-1/

API Gateway Path Transformation Service

Build a proxy middleware service that transforms incoming API request paths before forwarding them to backend services. The service should handle multiple path transformation scenarios including removing prefixes, adding base paths, and rewriting versioned API endpoints.

Requirements

Your service should create an Express application with three proxy middleware endpoints:

1. Legacy API Migration Endpoint

  • Mount a proxy at /api/v1
  • Forward requests to http://localhost:4001
  • Transform paths by:
    • Removing the /api/v1 prefix from incoming requests
    • Adding /legacy as a base path for the backend
    • Example: GET /api/v1/usersGET /legacy/users on backend

2. New API Version Endpoint

  • Mount a proxy at /api/v2
  • Forward requests to http://localhost:4002
  • Transform paths by:
    • Replacing /api/v2 with /v2/services
    • Example: GET /api/v2/productsGET /v2/services/products on backend

3. Admin Dashboard Endpoint

  • Mount a proxy at /admin
  • Forward requests to http://localhost:4003
  • Transform paths dynamically based on user role:
    • If the request includes an X-User-Role: superadmin header, prepend /superadmin to the path
    • Otherwise, prepend /standard to the path
    • Example with header: GET /admin/settingsGET /superadmin/settings on backend
    • Example without header: GET /admin/settingsGET /standard/settings on backend

Implementation Details

  • The Express server should listen on port 3000
  • Use appropriate middleware configuration options to enable origin changes
  • Implement all path transformations using the proxy middleware's path transformation capabilities

Test Cases

Test 1: Legacy API path transformation { .test }

@test

// Mock backend server on port 4001
const backend = express();
backend.get('/legacy/users', (req, res) => res.json({ path: req.path }));

// Test request
const response = await request(app)
  .get('/api/v1/users')
  .expect(200);

expect(response.body.path).toBe('/legacy/users');

Test 2: New API version path transformation { .test }

@test

// Mock backend server on port 4002
const backend = express();
backend.get('/v2/services/products', (req, res) => res.json({ path: req.path }));

// Test request
const response = await request(app)
  .get('/api/v2/products')
  .expect(200);

expect(response.body.path).toBe('/v2/services/products');

Test 3: Admin path transformation with superadmin role { .test }

@test

// Mock backend server on port 4003
const backend = express();
backend.get('/superadmin/settings', (req, res) => res.json({ path: req.path }));

// Test request
const response = await request(app)
  .get('/admin/settings')
  .set('X-User-Role', 'superadmin')
  .expect(200);

expect(response.body.path).toBe('/superadmin/settings');

Test 4: Admin path transformation without role header { .test }

@test

// Mock backend server on port 4003
const backend = express();
backend.get('/standard/settings', (req, res) => res.json({ path: req.path }));

// Test request
const response = await request(app)
  .get('/admin/settings')
  .expect(200);

expect(response.body.path).toBe('/standard/settings');

Implementation

@generates

API

/**
 * Creates and configures an Express application with proxy middleware endpoints
 * that transform request paths before forwarding to backend services.
 *
 * @returns {Express.Application} Configured Express application
 */
function createProxyService() {
  // Implementation here
}

module.exports = { createProxyService };

Dependencies { .dependencies }

express { .dependency }

Provides web server framework.

http-proxy-middleware { .dependency }

Provides proxy middleware with path transformation capabilities.

tile.json