ctx.response.type = 'json';
app.use()用来加载中间件
app.use(ctx => { ctx.body = 'Hello Koa'; });
以"先进后出"(first-in-last-out)的顺序执行。
  1. 最外层的中间件首先执行。
  2. 调用next函数,把执行权交给下一个中间件。
  3. ...
  4. 最内层的中间件最后执行。
  5. 执行结束后,把执行权交回上一层的中间件。
  6. ...
  7. 最外层的中间件收回执行权之后,执行next函数后面的代码。
const one = (ctx, next) => { console.log('>> one'); next(); console.log('<< one'); } const two = (ctx, next) => {
console.log('>> two'); next(); console.log('<< two'); } const three = (ctx, next) => { console.log('>> three'); next(); console.log('<< three'); } app.use(one); app.use(two); app.use(three);
有如下输出。
>> one >> two >> three << three << two << one
上面都是同步的中间件,异步的需要加async
const main = async function (ctx, next) { ctx.response.type = 'html'; ctx.response.body = await fs.readFile('./demos/template.html', 'utf8'); }; app.use(main);
上面代码中,fs.readFile是一个异步操作,必须写成await fs.readFile(),然后中间件必须写成 async 函数。
上下文
请求
响应
async function doWork() { // 使用 async/await 的方式依次获取两个 JSON 文件 var result1 = await fetch('data1.json'); var result2 = await fetch('data2.json'); return { result1, result2 }; }
这个语法可以理解为:被 async 关键字标记的函数,可以对其使用 await 关键字来暂停函数的执行直到异步操作结束。
async function indexStep1(ctx, next) { //逻辑处理第一部分 await next(); } async function indexStep2(ctx, next) { //逻辑处理第二部分 await next(); } async function indexStep3(ctx, next) { //逻辑处理第三部分 await ctx.render('index'); } router.get('/index', indexStep1, indexStep2, indexStep3);

同时实现get和post
router.all
Create routes with multiple HTTP methods using router.register():
app.register('/', ['get', 'post'], function *(next) { // ... });
Create route for all methods using router.all():
app.all('/', function *(next) { // ... });
获取参数
eventCenter?data1=leijh
var data = ctx.request.query.data || null;

koa介绍的更多相关文章

  1. 深入浅出Koa

    深入浅出Koa(1):生成器和Thunk函数 Koa是个小而美的Node.js web框架,它由Express的原班人马打造的, 致力于以一种现代化开发的方式构建web应用. 通过这个系列,你将能够理 ...

  2. Koa2和相关资料

    koa2是什么我就不介绍,这里只是收集一些有用的资料,koa这里默认就指koa2了额. koa介绍 koa(GitHub) koa(npm) 文档 Usage Guide Error Handling ...

  3. Koa 框架介绍

    Node.js 是一个异步的世界,官方 API 支持的都是 callback 形式的异步编程模型,这 会带来许多问题,例如:callback 嵌套问题 ,异步函数中可能同步调用 callback 返回 ...

  4. koa 核心源码介绍

    链接来源 Request,Context,Response  在代码运行之前就已经存在的 Request和Response自身的方法会委托到Context中. Context源码片段 var dele ...

  5. koa 框架 介绍 -- 待续

    对比 express  更小  更健壮 解决繁琐的回调函数嵌套, 并极大地提升错误处理的效率 Koa 的核心设计思路是为中间件层 提供高级语法糖封装, (其实就是用了 ES6的生成器, 能中断函数的执 ...

  6. 一份ChatBot开源工程介绍(H5 + WX + KOA)

    vue-mpvue-ChatRobot https://github.com/fanqingsong/vue-mpvue-ChatRobot 前端 : Vue + Mpvue(支持移动端与小程序) ; ...

  7. Koa框架实践与中间件原理剖析

     最近尝试用了一下Koa,并在此记录一下使用心得. 注意:本文是以读者已经了解Generator和Promise为前提在写的,因为单单Generator和Promise都能够写一篇博文来讲解介绍了,所 ...

  8. 三HttpServletResponse对象介绍(1)

    转载自http://www.cnblogs.com/xdp-gacl/p/3789624.html Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象 ...

  9. 深入探析koa之异步回调处理篇

    在上一篇中我们梳理了koa当中中间件的洋葱模型执行原理,并实现了一个可以让洋葱模型自动跑起来的流程管理函数.这一篇,我们再来研究一下koa当中异步回调同步化写法的原理,同样的,我们也会实现一个管理函数 ...

随机推荐

  1. 想控制GIF图片动画播放吗?试试gifffer.js

    在线演示:http://www.gbtags.com/gb/demoviewer/3578/c6bec39a-61ae-4017-8e23-e0bc1eeb740f/example|index.htm ...

  2. 右键菜单 GenericMenu

    http://www.cnblogs.com/zhaoqingqing/p/3799294.html 自定义窗口中使用右键菜单: // This example shows how to create ...

  3. 显示游戏FPS帧率的几种计算方式

    FPSDisplay.cs using UnityEngine; using System.Collections; public class FPSDisplay : MonoBehaviour { ...

  4. Python 多重循环

    # coding=gbk LOL1 = ['OMG', 'EDG', '皇族'] LOL2 = ['韩国', '欧洲', '美国'] for l1 in LOL1: for l2 in LOL2: p ...

  5. Nginx.conf简介

    vim /etc/nginx/nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log lo ...

  6. 深入理解C++的动态绑定和静态绑定

    转自:http://blog.csdn.net/chgaowei/article/details/6427731 为了支持c++的多态性,才用了动态绑定和静态绑定.理解他们的区别有助于更好的理解多态性 ...

  7. 小米电视2S加量不加价,你还会买吗?

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  8. Eddy&#39;s digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  9. jquery 获取css position的值

      jquery 获取css position的值 CreateTime--2018年5月28日14:03:12 Author:Marydon 1.情景展示 <div id="aa&q ...

  10. linux 查看 内存条具体信息, 几根内存条 命令

    sudo dmidecode | grep -A16 "Memory Device$" 需要root 权限.. [life@localhost mp3blaster-3.2.5]$ ...