## 路由koa-router——MVC 中重要的环节:Url 处理器 ## 🇨🇳 iKcamp 制作团队 原创作者:[大哼](https://github.com/pplgin)、[阿干](https://github.com/xiaoyaojun)、[三三](https://github.com/l3ve)、[小虎](https://github.com/tinyuen)、[胖子](https://github.com/pangz1)、[小哈](http://zoei.me/)、[DDU](https://github.com/DDU1222)、[可木](https://github.com/cfancc)、[晃晃](https://github.com/walterxu0704) 文案校对:[李益](https://github.com/yliiii)、[大力萌](https://github.com/yanyixin)、[Au](https://github.com/MatildaJin)、[DDU](https://github.com/DDU1222)、[小溪里](http://www.xiaoxili.com/)、[小哈](http://zoei.me/) 风采主播:[可木](https://github.com/cfancc)、[阿干](https://github.com/xiaoyaojun)、[Au](https://github.com/MatildaJin)、[DDU](https://github.com/DDU1222)、[小哈](http://zoei.me/) 视频剪辑:[小溪里](http://www.xiaoxili.com/) 主站运营:[给力xi](https://github.com/jackson13145)、[xty](https://github.com/xiatianyu) 教程主编:[张利涛](https://github.com/brucecham) --- 视频地址:[https://www.cctalk.com/v/15114923889954](https://www.cctalk.com/v/15114923889954) ![](https://user-gold-cdn.xitu.io/2017/12/25/1608b878831a4c8b?w=1608&h=968&f=png&s=838443) # 文章 # 路由 koa-router > 上一节我们学习了中间件的基本概念,本节主要带大家学习下 `koa-router` 路由中间件的使用方法。
路由是用于描述 `URL` 与处理函数之间的对应关系的。比如用户访问 `http://localhost:3000/`,那么浏览器就会显示 `index` 页面的内容,如果用户访问的是 `http://localhost:3000/home`,那么浏览器应该显示 `home` 页面的内容。
要实现上述功能,如果不借助 `koa-router` 或者其他路由中间件,我们自己去处理路由,那么写法可能如下所示: ```js const Koa = require('koa'); const app = new Koa(); app.use(async (ctx, next) => { if (ctx.request.path === '/') { ctx.response.body = '
把上述代码复制并覆盖到 `app.js` 中,然后执行以下命令启动 `node` 程序: ```js node app.js ```
启动之后在浏览器中分别访问 `http://localhost:3000/`、`http://localhost:3000/home`、`http://localhost:3000/404` 就能看到相应的页面了。
上述 `app.js` 的代码中,由 `async` 标记的函数称为『异步函数』,在异步函数中,可以用 `await` 调用另一个异步函数,`async` 和 `await` 这两个关键字将在 ES7 中引入。参数 `ctx` 是由 `koa` 传入的,我们可以通过它来访问 `request` 和 `response`,`next` 是 `koa` 传入的将要处理的下一个异步函数。 **注意:** 由于 `node` 在 `v7.6.0` 中才支持 `async` 和 `await`,所以在运行 `app.js` 之前请确保 node 版本正确,或者使用一些第三方的 `async` 库来支持。
这样的写法能够处理简单的应用,但是,一旦要处理的 `URL` 多起来的话就会显得特别笨重。所以我们可以借助 `koa-router` 来更简单的实现这一功能。 下面来介绍一下如何正确的使用 `koa-router`。
## 安装 koa-router 通过 `npm` 命令直接安装: ``` npm i koa-router -S ``` `-S` 或者 `--save` 是为了安装完成之后能够在 `package.json` 的 `dependencies` 中保留 `koa-router`,以便于下次只需要执行 `npm i` 或者 `npm install` 就能够安装所有需要的依赖包。
## 基本使用方法 如果要在 `app.js` 中使用 `koa-router` 来处理 `URL`,可以通过以下代码来实现: ```js const Koa = require('koa') // 注意 require('koa-router') 返回的是函数: const router = require('koa-router')() const app = new Koa() // 添加路由 router.get('/', async (ctx, next) => { ctx.response.body = `
路由是用于描述 `URL` 与处理函数之间的对应关系的。比如用户访问 `http://localhost:3000/`,那么浏览器就会显示 `index` 页面的内容,如果用户访问的是 `http://localhost:3000/home`,那么浏览器应该显示 `home` 页面的内容。
要实现上述功能,如果不借助 `koa-router` 或者其他路由中间件,我们自己去处理路由,那么写法可能如下所示: ```js const Koa = require('koa'); const app = new Koa(); app.use(async (ctx, next) => { if (ctx.request.path === '/') { ctx.response.body = '
index page
'; } else { await next(); } }); app.use(async (ctx, next) => { if (ctx.request.path === '/home') { ctx.response.body = 'home page
'; } else { await next(); } }); app.use(async (ctx, next) => { if (ctx.request.path === '/404') { ctx.response.body = '404 Not Found
'; } else { await next(); } }); app.listen(3000, ()=>{ console.log('server is running at http://localhost:3000') }) ```把上述代码复制并覆盖到 `app.js` 中,然后执行以下命令启动 `node` 程序: ```js node app.js ```
启动之后在浏览器中分别访问 `http://localhost:3000/`、`http://localhost:3000/home`、`http://localhost:3000/404` 就能看到相应的页面了。
上述 `app.js` 的代码中,由 `async` 标记的函数称为『异步函数』,在异步函数中,可以用 `await` 调用另一个异步函数,`async` 和 `await` 这两个关键字将在 ES7 中引入。参数 `ctx` 是由 `koa` 传入的,我们可以通过它来访问 `request` 和 `response`,`next` 是 `koa` 传入的将要处理的下一个异步函数。 **注意:** 由于 `node` 在 `v7.6.0` 中才支持 `async` 和 `await`,所以在运行 `app.js` 之前请确保 node 版本正确,或者使用一些第三方的 `async` 库来支持。
这样的写法能够处理简单的应用,但是,一旦要处理的 `URL` 多起来的话就会显得特别笨重。所以我们可以借助 `koa-router` 来更简单的实现这一功能。 下面来介绍一下如何正确的使用 `koa-router`。
## 安装 koa-router 通过 `npm` 命令直接安装: ``` npm i koa-router -S ``` `-S` 或者 `--save` 是为了安装完成之后能够在 `package.json` 的 `dependencies` 中保留 `koa-router`,以便于下次只需要执行 `npm i` 或者 `npm install` 就能够安装所有需要的依赖包。
## 基本使用方法 如果要在 `app.js` 中使用 `koa-router` 来处理 `URL`,可以通过以下代码来实现: ```js const Koa = require('koa') // 注意 require('koa-router') 返回的是函数: const router = require('koa-router')() const app = new Koa() // 添加路由 router.get('/', async (ctx, next) => { ctx.response.body = `