2020 全站之路-Node.js 系列
"use strict";
const log = console.log;
const vscodeInspectorNode = () => {
let a = 10;
const A = a ** 10;
log(`A`, A);
};
vscodeInspectorNode();
koa server
const Koa = require("koa");
const app = new Koa();
const log = console.log;
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get("X-Response-Time");
log(`\nmiddleware 1, 后执行`, rt);
log(`${ctx.method} ${ctx.url} - ${rt}`);
});
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
log(`\nmiddleware 2, 先执行`, `${ms}ms`);
ctx.set("X-Response-Time", `${ms}ms`);
});
app.use(async (ctx, next) => {
log(`\nmiddleware 0, 最后的 middleware 最先执行`);
ctx.body = "Hello World! <br />powered by koa.js";
});
app.listen(3000);
打开App,阅读手记