Node.js-koa入门
2017-08-17 14:00
351 查看
摘要 - 廖雪峰JavaScript教程
其中,参数
上面的异步函数中,我们首先用
由
注意:要是想运行上面的代码,请使用最新的node7以后的版本,因为低版本的node不支持
每收到一个
我们可以对
原因是koa把很多
例如,可以用以下3个
此外,如果一个
理解了
最后注意
创建一个koa2工程
首先,我们创建一个test目录,并在目录里面创建一个
koa.js文件,输入以下代码:
// 导入的是koa2,不是koa1,koa2是一个class类,所以大写。 const Koa = require('koa') // 创建一个web app 实例 const app = new Koa() // 对于任何请求,都执行该回调函数 app.use(async (ctx, next) => { await next() ctx.response.type = 'text/html' ctx.response.body = '<h1> Hello Koa2 </h1>' }) // 监听9000端口 app.listen(9000, () => { console.log('Server is running and the port is 9000 ') })
其中,参数
ctx是koa传入的封装了
response和
resquest的变量,
next是koa要处理的下一个异步函数。
上面的异步函数中,我们首先用
await next( )处理下一个异步函数,然后设置
Content-Type和内容。
由
async标记的函数称为异步函数,在异步函数中,可以用
await调用另一个异步函数,这两个关键字在ES7中引入。
注意:要是想运行上面的代码,请使用最新的node7以后的版本,因为低版本的node不支持
async和
await关键,或者你可以
babel进行转成es5进行运行。
koa middleware
让我们在仔细看下koa的执行逻辑。核心代码是:app.use(async (ctx, next) => { await next() ctx.response.type = 'text/html' ctx.response.body = '<h1> Hello Koa2 </h1>' })
每收到一个
http请求,koa就会调用通过
app.use()注册的异步函数,并传入
ctx和
next参数。
我们可以对
ctx操作,并设置返回内容。但是为什么要调用
await next()?
原因是koa把很多
async函数组成一个处理链,每个
async函数都可以做一些自己的事情,然后用
await next()来调用下一个
async函数。我们把每个
async函数称为
middleware,这些
middleware可以组合起来,完成很多有用的功能。
例如,可以用以下3个
middleware组成处理链,依次打印日志,记录处理时间,输出HTML:
app.use(async (ctx, next) => { console.log(`${ctx.request.method} ${ctx.request.url}`); // 打印URL await next(); // 调用下一个middleware }); app.use(async (ctx, next) => { const start = new Date().getTime(); // 当前时间 await next(); // 调用下一个middleware const ms = new Date().getTime() - start; // 耗费时间 console.log(`Time: ${ms}ms`); // 打印耗费时间 }); app.use(async (ctx, next) => { await next(); ctx.response.type = 'text/html'; ctx.response.body = '<h1>Hello, koa2!</h1>'; });
middleware的顺序很重要,也就是调用
app.use()的顺序决定了
middleware的顺序。
此外,如果一个
middleware没有调用
await next(),会怎么办?答案是后续的
middleware将不再执行了。这种情况也很常见,例如,一个检测用户权限的
middleware可以决定是否继续处理请求,还是直接返回403错误:
app.use(async (ctx, next) => { if (await checkUserPermission(ctx)) { await next(); } else { ctx.response.status = 403; } });
理解了
middleware,我们就基本会用koa了!
最后注意
ctx对象有一些简写的方法,例如
ctx.url相当于
ctx.request.url,
ctx.type相当于
ctx.response.type。
相关文章推荐
- node.js之web开发 koa入门
- node.js之web开发 koa入门
- node.js 入门实例【转】
- Node.js、Express、Socket.io 入门
- Node.js学习--基础知识(1)--入门
- 从零开始node.js入门项目(五)后台管理
- Node.JS 入门
- Node.js 入门简介 第一个应用
- Node.js入门(含NVM、NPM、NVM的安装)
- node.js 入门示例
- Node.js入门总结
- Node.js入门
- [转] Node.js 框架比较: Express vs. Koa vs. Hapi
- node.js 从入门到。。。
- Java开发者结合Node.js编程入门教程
- Node.js入门
- Node.js-入门五--path优化
- 2.node.js 入门
- node.js入门 - 1.我们的第一个服务
- Node.js入门实例程序