为了账号安全,请及时绑定邮箱和手机立即绑定

2020 全站之路-Node.js 系列

标签:
Node.js Html5 CSS3

2020 全站之路-Node.js 系列

// TODO: 2020 全站之路-Node.js 系列

"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description vscode & debug node
* @augments
* @example
*
*/



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;
// logger
app.use(async (ctx, next) => {
  await next();
  // middleware 1, 后执行
  const rt = ctx.response.get("X-Response-Time");
  log(`\nmiddleware 1, 后执行`, rt);
  log(`${ctx.method} ${ctx.url} - ${rt}`);
});

// x-response-time
app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  // middleware 2, 先执行
  const ms = Date.now() - start;
  log(`\nmiddleware 2, 先执行`, `${ms}ms`);
  ctx.set("X-Response-Time", `${ms}ms`);
});

// response
app.use(async (ctx, next) => {
  // log(`response ctx`, ctx);
  // await next();
  log(`\nmiddleware 0, 最后的 middleware 最先执行`);
  ctx.body = "Hello World! <br />powered by koa.js";
});

app.listen(3000);
// app.listen(8080);

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
架构师
手记
粉丝
39
获赞与收藏
682

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消