我爱编程

Koa2

2018-04-10  本文已影响0人  Gukson666

Koa 必须使用 7.6 以上的版本。如果你的版本低于这个要求,就要先升级 Node。

基本用法

Koa 提供一个 Context 对象,表示一次对话的上下文(包括 HTTP 请求和 HTTP 回复)。通过加工这个对象,就可以控制返回给用户的内容。
Context.response.body属性就是发送给用户的内容。如下:

const Koa = require('koa');
const app = new Koa();
const main = ctx => {
  ctx.response.body = 'Hello World';
};
app.use(main);
app.listen(3000);    //架设http服务器

app.listen()就是http.createServer(app.callback()).listen(...)的缩写。

main函数用来设置ctx.response.body。然后,使用app.use方法加载main函数。
ctx.response代表 HTTP Response。同样地,ctx.request代表 HTTP Request。

Koa 默认的返回类型是text/plain,如果想返回其他类型内容,可以用ctx.request.accepts判断一下,客户端希望接受什么数据(根据 HTTP Request 的Accept字段),然后使用ctx.response.type指定返回类型。

const main = ctx => {
  if (ctx.request.accepts('xml')) {
    ctx.response.type = 'xml';
    ctx.response.body = '<data>Hello World</data>';
  } else if (ctx.request.accepts('json')) {
    ctx.response.type = 'json';
    ctx.response.body = { data: 'Hello World' };
  } else if (ctx.request.accepts('html')) {
    ctx.response.type = 'html';
    ctx.response.body = '<p>Hello World</p>';
  } else {
    ctx.response.type = 'text';
    ctx.response.body = 'Hello World';
  }
};

路由

原生路由

通过ctx.request.path可以获取用户请求的路径,由此实现简单的路由。

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';
  }
};

koa-route 模块

原生路由用起来不太方便,我们可以使用封装好的[koa-route]模块。

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。

静态资源

静态资源一个个写路由很麻烦,也没必要。[koa-static]模块封装了这部分的请求。

const path = require('path');
const serve = require('koa-static');
const main = serve(path.join(__dirname));
app.use(main);

重定向

服务器需要重定向(redirect)访问请求,ctx.response.redirect()方法可以发出一个302跳转,将用户导向另一个路由。

const redirect = ctx => {
  ctx.response.redirect('/');
  ctx.response.body = '<a href="/">Index Page</a>';
};
app.use(route.get('/redirect', redirect));

中间件

const logger = (ctx, next) => {
  console.log(`${Date.now()} ${ctx.request.method} ${ctx.request.url}`);
  next();
}
app.use(logger);

上面代码中的logger函数就叫做"中间件"(middleware),因为它处在 HTTP Request 和 HTTP Response 中间,用来实现某种中间功能。app.use()用来加载中间件。

Koa 所有的功能都是通过中间件实现的,默认接受两个参数,第一个参数是 Context 对象,第二个参数是next函数。只要调用next函数,就可以把执行权转交给下一个中间件。
多个中间件会形成一个栈结构(middle stack),以"先进后出"(first-in-last-out)的顺序执行。如果中间件内部没有调用next函数,那么执行权就不会传递下去。

如果有异步操作(比如读取数据库),中间件就必须写成 [async 函数]。

const fs = require('fs.promised');
const Koa = require('koa');
const app = new Koa();
const main = async function (ctx, next) {
  ctx.response.type = 'html';
  ctx.response.body = await fs.readFile('./demos/template.html', 'utf8');
};
app.use(main);
app.listen(3000);

中间件的合成[koa-compose]模块可以将多个中间件合成为一个。

const middlewares = compose([logger, main]);
app.use(middlewares);

Koa 提供了ctx.throw()方法,用来抛出错误。ctx.throw(500)就是抛出500错误。

const main = ctx => {
  ctx.throw(500);
};

如果将ctx.response.status设置成404,就相当于ctx.throw(404),返回404错误。

const main = ctx => {
  ctx.response.status = 404;
  ctx.response.body = 'Page Not Found';
};

为了方便处理错误,最好使用try...catch将其捕获。但是,为每个中间件都写try...catch太麻烦,我们可以让最外层的中间件,负责所有中间件的错误处理。

const handler = async (ctx, next) => {
  try {
    await next();  // 此处包住了后面内部所有中间件
  } catch (err) {
    ctx.response.status = err.statusCode || err.status || 500;
    ctx.response.body = {
      message: err.message
    };
  }
};
const main = ctx => {
  ctx.throw(500);
};
app.use(handler);
app.use(main);

Web App 的功能

Cookies

ctx.cookies用来读写 Cookie。

const main = function(ctx) {
  const n = Number(ctx.cookies.get('view') || 0) + 1;
  ctx.cookies.set('view', n);
  ctx.response.body = n + ' views';
}
// 访问 http://127.0.0.1:3000 ,你会看到1 views。刷新一次页面,就变成了2 views。

表单就是 POST 方法发送到服务器的键值对。[koa-body]模块可以用来从 POST 请求的数据体里面提取键值对。

const koaBody = require('koa-body');
const main = async function(ctx) {
  const body = ctx.request.body;
  if (!body.name) ctx.throw(400, '.name required');
  ctx.body = { name: body.name };
};
app.use(koaBody());

[koa-body]模块还可以用来处理文件上传。请看:
https://github.com/ruanyf/jstutorial/blob/gh-pages/nodejs/koa.md 阮一峰教程

级联式(Cascading)的结构,也就是说,属于是层层调用,第一个中间件调用第二个中间件,第二个调用第三个,以此类推。上游的中间件必须等到下游的中间件返回结果,才会继续执行,这点很像递归。

Koa2与Koa1的区别

简单来说就是async取代了星号* await取代了yield;
async/await 的特点:
可以让异步逻辑用同步写法实现
最底层的await返回需要是Promise对象
可以通过多层 async function 的同步写法代替传统的callback嵌套

Koa2特性:

const Koa = require('koa')
const app = new Koa()
// 简单例子:
app.use( async ( ctx ) => {
  let url = ctx.request.url
  ctx.body = url
})
app.listen(3000)

koa-router中间件

npm/cnpm install --save koa-router

const Koa = require('koa')
const fs = require('fs')
const app = new Koa()
const Router = require('koa-router')
let home = new Router()
// 子路由1
home.get('/', async ( ctx )=>{
  let html = `
    <ul>
      <li><a href="/page/helloworld">/page/helloworld</a></li>
      <li><a href="/page/404">/page/404</a></li>
    </ul>
  ctx.body = html
})
// 子路由2
let page = new Router()
page.get('/404', async ( ctx )=>{
  ctx.body = '404 page!'
}).get('/helloworld', async ( ctx )=>{
  ctx.body = 'helloworld page!'
})
// 装载所有子路由
let router = new Router()
router.use('/', home.routes(), home.allowedMethods())
router.use('/page', page.routes(), page.allowedMethods())
// 加载路由中间件
app.use(router.routes()).use(router.allowedMethods())

app.listen(3000, () => {
  console.log('[demo] route-use-middleware is starting at port 3000')
})

官网技术说明

Koa 依赖 node v7.6.0 或 ES2015及更高版本和 async 方法支持.
必修的 hello world 应用:

const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
  ctx.body = 'Hello World';
});
app.listen(3000);

级联通过一系列功能直接传递控制,直到一个返回,Koa 调用“下游”,然后控制流回“上游”。当一个中间件调用 next() 则该函数暂停并将控制传递给定义的下一个中间件。当在下游没有更多的中间件执行后,堆栈将展开并且每个中间件恢复执行其上游行为。

app.use(function) 将给定的中间件方法添加到此应用程序。
app.keys= 设置签名的 Cookie 密钥。

上下文(Context)

Koa Context 将 node 的 request 和 response 对象封装到单个对象中,为编写 Web 应用程序和 API 提供了许多有用的方法。 这些操作在 HTTP 服务器开发中频繁使用。

app.use(async ctx => {
  ctx; // 这是 Context
  ctx.request; // 这是 koa Request
  ctx.response; // 这是 koa Response
});

为方便起见许多上下文的访问器和方法直接委托给它们的 ctx.request或 ctx.response。ctx.type 和 ctx.length 委托给 response 对象,ctx.path 和 ctx.method 委托给 request。

ctx.req // Node 的 request 对象.
ctx.res // Node 的 response 对象.

绕过 Koa 的 response 处理是不被支持的.应避免使用以下 node 属性:
res.statusCode
res.writeHead()
res.write()
res.end()

ctx.request koa 的 Request 对象.
ctx.response koa 的 Response 对象.

ctx.cookies.get(name, [options]) 通过 options 获取 cookie name:
signed 所请求的cookie应该被签名
ctx.cookies.set(name, value, [options])通过 options 设置 cookie name 的 value :

request.header 请求标头对象。
request.header = 设置请求标头对象。

request.method 请求方法。
request.method = 设置请求方法,对于实现诸如 methodOverride() 的中间件是有用的。

request.url 获取请求 URL.
request.url = 设置请求 URL, 对 url 重写有用。

request.origin 获取URL的来源,包括 protocol 和 host。
ctx.request.origin // => http://example.com

request.href 获取完整的请求URL,包括 protocol,host 和 url。
ctx.request.href;// => http://example.com/foo/bar?q=1

request.path 获取请求路径名。

request.querystring 根据 ? 获取原始查询字符串.

request.type 获取请求 Content-Type 不含参数 "charset"。
const ct = ctx.request.type;// => "image/png"

request.query 获取解析的查询字符串, 当没有查询字符串时,返回一个空对象。
例如 "color=blue&size=small":

{
  color: 'blue',
  size: 'small'
}

request.accepts(types)
检查给定的 type(s) 是否可以接受,如果 true,返回最佳匹配,否则为 false。

response.header 响应标头对象。

response.status
获取响应状态。默认情况下,response.status 设置为 404 而不是像 node 的 res.statusCode 那样默认为 200。

response.message 获取响应的状态消息.
response.body 获取响应主体。

response.redirect(url, [alt]) 执行 [302] 重定向到 url.

response.type 获取响应 Content-Type 不含参数 "charset"。
const ct = ctx.type;// => "image/png"

response.length
以数字返回响应的 Content-Length,或者从ctx.body推导出来,或者undefined。


起步填坑

项目生成器: npm install -g koa-generator
在你的工作目录下,输入:koa2 HelloKoa2
成功创建项目后,进入项目目录,并执行npm install命令cd HelloKoa2 npm install
启动项目:npm start

koa声明说要在v3版本中取消对generator中间件的支持,所以为了长久考虑还是用async语法的好。
如果想要继续使用function*语法,可以使用 koa-convert 这个中间件进行转换。

const convert = require('koa-convert');
app.use(convert(bodyparser));
app.use(convert(json()));
app.use(convert(logger()));

Context封装了node中的request和response。
koa@1.x使用this引用Context对象:

app.use(function *(){
  this.body = 'Hello World';
});

koa@2.x中使用ctx来访问Context对象:

app.use(async (ctx, next) => {
  await next();
  ctx.body = 'Hello World';
});

项目配置

这里的配置指的是运行环境的配置,比如我们在开发阶段使用本地的数据库,测试要使用测试库,发布上线时候使用线上的库,也会有不同的端口号。
npm start 就会运行package.json中scripts对象对应的start字段后面的内容。

在npm中,有四个常用的缩写
npm start是npm run start
npm stop是npm run stop的简写
npm test是npm run test的简写
npm restart是npm run stop && npm run restart && npm run start的简写

上一篇 下一篇

猜你喜欢

热点阅读