UNPKG

@reedchan/koa-http-proxy

Version:
116 lines (98 loc) โ€ข 3.76 kB
"use strict"; const Koa = require("koa"); const proxy = require("../index"); const app = new Koa(); console.log("๐ŸŽฏ Koa Native Middleware Pattern Demo"); console.log(""); // Middleware counter to verify execution flow let middlewareCount = 0; // Route 1: Don't pass next - stop after proxy app.use(async (ctx, next) => { if (ctx.path.startsWith("/stop-after-proxy")) { console.log("๐Ÿ›‘ Don't pass next parameter - stop after proxy"); // ๐ŸŽฏ Key: Don't pass next parameter return proxy("https://httpbin.org", { debug: true, proxyReqPathResolver: (ctx) => { // Transform /stop-after-proxy/get to /get return ctx.path.replace("/stop-after-proxy", ""); }, })(ctx); // Note: didn't pass next } await next(); }); // Route 2: Pass next - continue after proxy app.use(async (ctx, next) => { if (ctx.path.startsWith("/continue-after-proxy")) { console.log("โ–ถ๏ธ Pass next parameter - continue after proxy"); // ๐ŸŽฏ Key: Pass next parameter return proxy("https://httpbin.org", { debug: true, proxyReqPathResolver: (ctx) => { // Transform /continue-after-proxy/get to /get return ctx.path.replace("/continue-after-proxy", ""); }, })(ctx, next); // Note: passed next } await next(); }); // Route 3: Dynamically decide whether to continue app.use(async (ctx, next) => { if (ctx.path.startsWith("/dynamic")) { const shouldContinue = ctx.query.continue === "true"; console.log(`๐Ÿ”„ Dynamic decision: ${shouldContinue ? "continue execution" : "stop execution"}`); const proxyFn = proxy("https://httpbin.org", { debug: true, proxyReqPathResolver: (ctx) => { // Transform /dynamic/get to /get return ctx.path.replace("/dynamic", ""); }, }); if (shouldContinue) { return proxyFn(ctx, next); // Pass next } else { return proxyFn(ctx); // Don't pass next } } await next(); }); // Second middleware: verify if executed app.use(async (ctx, next) => { middlewareCount++; console.log(`๐Ÿ“Š Second middleware executed! Count: ${middlewareCount}`); await next(); }); // Third middleware: final response (if no previous response) app.use(async (ctx, next) => { if (!ctx.body && !ctx.headerSent) { ctx.body = "โœ… All middleware executed - this is default response"; console.log("๐Ÿ“ Third middleware: set default response"); } await next(); }); app.listen(3005, () => { console.log("๐Ÿš€ Koa Native Pattern Example Server started on port 3005"); console.log(""); console.log("๐Ÿงช Test Scenarios:"); console.log(""); console.log("1๏ธโƒฃ Don't pass next - stop after proxy:"); console.log(" curl http://localhost:3005/stop-after-proxy/get"); console.log(" โ†’ โŒ Won't see 'Second middleware executed' log"); console.log(""); console.log("2๏ธโƒฃ Pass next - continue after proxy:"); console.log(" curl http://localhost:3005/continue-after-proxy/get"); console.log(" โ†’ โœ… Will see 'Second middleware executed' log"); console.log(""); console.log("3๏ธโƒฃ Dynamic control - don't continue:"); console.log(" curl http://localhost:3005/dynamic/get"); console.log(" โ†’ โŒ Won't see 'Second middleware executed' log"); console.log(""); console.log("4๏ธโƒฃ Dynamic control - continue:"); console.log(" curl 'http://localhost:3005/dynamic/get?continue=true'"); console.log(" โ†’ โœ… Will see 'Second middleware executed' log"); console.log(""); console.log("๐Ÿ’ก Core Concepts:"); console.log(" โ€ข proxy()(ctx) โ†’ Stop after proxy"); console.log(" โ€ข proxy()(ctx, next) โ†’ Continue after proxy"); console.log(" โ€ข Fully follows Koa native middleware pattern!"); });