var Koa = require("koa");
var Router = require("@koa/router");
const PORT = 3000;
var app = new Koa();
var router = new Router();
const requestLogger = async (ctx, next) => {
await next();
ctx.app.emit(</span>response<span class="pl-pds">
, ctx);
};
app.use(requestLogger);
router.get("/", (ctx, next) => {
ctx.body = "hello world";
});
router.get("/api", (ctx, next) => {
ctx.body = {
status: 0,
foo: "bar"
};
});
router.get("/test", (ctx, next) => {
ctx.body = "hello test";
});
router.get("/monitor", async ctx => {
const res = ctx.res;
ctx.status = 200;
res.setHeader("Content-Type", "text/html");
res.write(</span><h3>net monitor<h3><span class="pl-pds">
);
return new Promise(() => {
ctx.app.on("response", data => {
res.write(</span></span> <span class="pl-s"> <details></span> <span class="pl-s"> <summary></span> <span class="pl-s"> <a hre="<span class="pl-s1"><span class="pl-pse">${</span><span class="pl-smi">data</span>.<span class="pl-smi">url</span><span class="pl-pse">}</span></span>"><span class="pl-s1"><span class="pl-pse">${</span><span class="pl-smi">data</span>.<span class="pl-smi">url</span><span class="pl-pse">}</span></span></a></span> <span class="pl-s"> </summary></span> <span class="pl-s"> <pre><span class="pl-s1"><span class="pl-pse">${</span><span class="pl-c1">JSON</span>.<span class="pl-c1">stringify</span>(<span class="pl-smi">data</span>.<span class="pl-c1">body</span>, <span class="pl-c1">null</span>, <span class="pl-c1">2</span>)<span class="pl-pse">}</span></span><pre></span> <span class="pl-s"> </details></span> <span class="pl-s"> <span class="pl-pds">
);
});
});
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(PORT);
console.info(</span>server started at http://localhost:<span class="pl-s1"><span class="pl-pse">${</span><span class="pl-c1">PORT</span><span class="pl-pse">}</span></span><span class="pl-pds">
);