@reedchan/koa-http-proxy
Version:
http proxy middleware for koa
116 lines (98 loc) โข 3.76 kB
JavaScript
;
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!");
});