koa框架使用配置工作
-
首先创建一个文件夹,然后初始化 package.json :
npm init -y
-
安装koa2:
cnpm i koa --save
context对象
Koa 提供一个 Context对象,表示一次对话的上下文(包括 HTTP 请求和HTTP回复)。通过加工这个对象,就可以控制返回给用户的内容。
// demos/02.js
const Koa = require('koa');
const app = new Koa();
const main = ctx => {
ctx.response.body = 'Hello World';
};
app.use(main);
app.listen(3000);
上面代码中,main函数用来设置ctx.response.body。然后,使用app.use方法加载main函数。
其中
- ctx.request代表http.request
- ctx.response代表http.response
get请求
在Koa2中GET请求可以通过request接受url的参数,
但接受的方式有两种:
- query:返回的是格式化后的参数对象
- querystring:返回的请求字符串
我们可以由两种方式来获取GET请求,一种是通过 ctx.request 来获取GET请求,一种则是直接在ctx中得到GET请求:
post请求
在 Koa2 中,没有给对于 POST 请求的处理封装方便的获取参数的方法,需要通过通过解析上下文 context 中的元素 node.js 请求对象 req 来获取。因此获取POST请求的步骤可以理解为以下三步:
- 解析上下文 ctx 中的原生 node.js 对象 req。
- 将POST表单数据解析成 query string 字符串。
- 将字符串转换成 JSON 格式。
Koa 的洋葱模型介绍
// 洋葱模型特点
// 引入 Koa
const Koa = require("koa");
// 创建服务
const app = new Koa();
app.use(async (ctx, next) => {
console.log(1);
await next();
console.log(2);
});
app.use(async (ctx, next) => {
console.log(3);
await next();
console.log(4);
});
app.use(async (ctx, next) => {
console.log(5);
await next();
console.log(6);
});
// 监听服务
app.listen(3000);
// 1
// 3
// 5
// 6
// 4
// 2
我们知道 Koa 的 use 方法是支持异步的,所以为了保证正常的按照洋葱模型的执行顺序执行代码,需要在调用 next 的时候让代码等待,等待异步结束后再继续向下执行,所以我们在 Koa 中都是建议使用 async/await 的,引入的中间件都是在 use 方法中调用,由此我们可以分析出每一个 Koa 的中间件都是返回一个 async 函数的。
网页模板
1.4 网页模板
实际开发中,返回给用户的网页往往都写成模板文件。我们可以让 Koa 先读取模板文件,然后将这个模板返回给用户。
// demos/04.js
const fs = require('fs');
const main = ctx => {
ctx.response.type = 'html';
ctx.response.body = fs.createReadStream('./demos/template.html');
};
运行这个 Demo。
$ node demos/04.js
访问 http://127.0.0.1:3000,看到的就是模板文件)的内容了。
koa中间件使用
https://blog.youkuaiyun.com/sinat_17775997/article/details/82898312
路由
- 原生路由
网站一般都有多个页面。通过ctx.request.path可以获取用户请求的路径,由此实现简单的路由。请看下面的例子
// demos/05.js
const main = ctx => {
if (ctx.request.path !== '/') {
ctx.response.type = 'html';
ctx.response.body = '<a href="/">Index Page</a>';
} else {
ctx.response.body = 'Hello World';
}
};
运行这个 demo。
$ node demos/05.js
访问 http://127.0.0.1:3000/about ,可以看到一个链接,点击后就跳到首页。
- koa-router 框架
原生路由用起来不太方便,我们可以使用封装好的koa-route模块。请看下面的例子
// demos/06.js
const route = require('koa-route');
const about = ctx => {
ctx.response.type = 'html';
ctx.response.body = '<a href="/">Index Page</a>';
};
const main = ctx => {
ctx.response.body = 'Hello World';
};
app.use(route.get('/', main));
app.use(route.get('/about', about));
上面代码中,根路径 / 的处理函数是main,/about路径的处理函数是about。
运行这个 demo。
$ node demos/06.js
访问 http://127.0.0.1:3000/about ,效果与上一个例子完全相同。
- 静态资源
如果网站提供静态资源(图片、字体、样式表、脚本…),为它们一个个写路由就很麻烦,也没必要。koa-static模块封装了这部分的请求。请看下面的例子。
// demos/12.js
const path = require('path');
const serve = require('koa-static');
const main = serve(path.join(__dirname));
app.use(main);
运行这个 Demo。
$ node demos/12.js
访问 http://127.0.0.1:3000/12.js,在浏览器里就可以看到这个脚本的内容。
- 重定向
有些场合,服务器需要重定向(redirect)访问请求。比如,用户登陆以后,将他重定向到登陆前的页面。ctx.response.redirect()方法可以发出一个302跳转,将用户导向另一个路由。请看下面的例子。
// demos/13.js
const redirect = ctx => {
ctx.response.redirect('/');
ctx.response.body = '<a href="/">Index Page</a>';
};
app.use(route.get('/redirect', redirect));
运行这个 demo。
$ node demos/13.js
访问 http://127.0.0.1:3000/redirect ,浏览器会将用户导向根路由。