框架结构例子

https://github.com/bayi-lzp/koa-template

官网例子(有很多 示例)

https://github.com/koajs/examples

《Koa2进阶学习笔记》电子书

https://github.com/chenshenhai/koa2-note

实战项目1

  • Node.js + Koa2 + MySQL 开发的一套完整 RESTful API
  • Vue.js + element-ui 管理后台
  • SSR Nuxtjs 前台服务端渲染

https://github.com/lfb/nodejs-koa-blog
实战项目2

https://gitee.com/caiheping/koa-template

https://gitee.com/caiheping/vue-template

项目的结构都可以借鉴。

tip: 注意,不要把 static 中间件放到 Koa 的全局中间件上(如果对于每个请求都需要判断一次是不是静态资源,会影响 QPS),最好结合 koa-router 来处理,按需挂在,上代码。

router.get('/public/', async (ctx, next) => {
ctx.url = path.basename(ctx.url)

  await next()
}, staticServer(resolve('./public'), {gzip: true}))

koa-router提供一种router.prefix方法,此方法对于某一个router来说,是一个全局配置,此router的所有路径都会自动被添加该前缀。

const Koa = require(‘koa‘)
const app = new Koa()
// 引入koa-router
const router = require(‘koa-router‘)
// 这两行代码等同于 const router1 = require(‘koa-router‘)()
const router1 = new router()
// 为router1配置路由前缀
router1.prefix(‘/pre‘)
router1.get(‘/get‘, function (ctx, next) {
ctx.body = ‘this is a get1 response!‘
})
// router2不配置路由前缀
const router2 = new router()
router2.get(‘/get‘, function (ctx, next) {
ctx.body = ‘this is a get2 response!‘
})
// 注册路由
app.use(router1.routes(), router1.allowedMethods())
app.use(router2.routes(), router2.allowedMethods()) app.listen(8000) module.exports = app


使用router.use方法,同样的能够为路由分层,并且不会因为忽略了prefix的全局配置造成一些不必要的失误,
推荐使用这一种方法为路由分层

const Koa = require(‘koa‘)
const app = new Koa()
const router = require(‘koa-router‘)
// 定义子路由
const router_children = new router()
router_children.get(‘/get‘, function (ctx, next) {
ctx.body = ‘this is a get response from router.use!‘
})
// 根路由
const router_root = new router()
// 在根路由中注册子路由
router_root.use(‘/root‘, router_children.routes(), router_children.allowedMethods())
// 在app中注册根路由
app.use(router_root.routes(), router_root.allowedMethods())
app.listen(8000) module.exports = app


nodejs+koa 后台框架结构、demo学习地址的更多相关文章

  1. nodejs+koa+uniapp实现微信小程序登陆获取用户手机号及openId

    nodejs+koa+uniapp实现微信小程序登陆获取用户手机号及openId 前言: 我准备用nodejs+koa+uniapp实现一款餐饮点单小程序,以及nodejs+koa+vue实现后端管理 ...

  2. 学习地址(oraclemysqllinux)

    1.安装配置 http://blog.chinaunix.net/uid-27126319-id-3466193.htmlhttp://www.cnblogs.com/gaojun/archive/2 ...

  3. [Unity3D]做个小Demo学习Input.touches

    [Unity3D]做个小Demo学习Input.touches 学不如做,下面用一个简单的Demo展示的Input.touches各项字段,有图有真相. 本项目已发布到Github,地址在(https ...

  4. NodeJS,MongoDB,Vue,VSCode 集成学习

    NodeJS,MongoDB,Vue,VSCode 集成学习 开源项目地址:http://www.mangdot.com

  5. 如何写一套下拉刷新的控件?《MJRefresh原理浅析》(附Demo下载地址)

    相信大家有很多人在做项目的时候都在使用MJRefresh 控件来实现下拉刷新的功能: MJRefresh经过不断的重构与更新迭代,现在不管是功能上还是代码结构上都是相当不错的,都是很值我们去学习的. ...

  6. iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)

    由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付功能加到了自己的代码中.一些根据文档来 ...

  7. 转:iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)

    iosiOSIOS文档服务器测试电话 由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付 ...

  8. layui学习地址

    --layui学习地址 ,相当之好用,非常感谢为我们工作和学习提供方便的才子们,谢谢~https://www.layui.com/demo/layim.html

  9. React 学习笔记(学习地址汇总)

    好的博文地址:http://www.ruanyifeng.com/blog/2015/03/react.html 官网学习地址:http://facebook.github.io/react/docs ...

  10. 《IT蓝豹》吹雪花demo,学习android传感器

    吹雪花demo,学习android传感器 吹雪花demo,学习android传感器,嘴巴对着手机底部吹一下就会出现飘着雪花效果. 算是学习android传感器效果.本例子主要是通过android.me ...

随机推荐

  1. Mac技术服务, 感谢 点赞打赏

    ​ 首先,非常感谢大家对我的支持和鼓励. 如果需要单独解决Mac问题,您也可以选择付费服务,感谢大家的理解与支持! * 普通问题/软件安装:10元--100元每个(根据具体问题由您而定): * 普通问 ...

  2. 【易语言】exui超级列表框使用方法

    优秀例程1 黑鸟 https://jiucaiwl.lanzoum.com/iLq8B0oswkuf

  3. keil调试教程

    点击跳转 如果开启调试就提示弹框错误2k,说明你没有破解你的keil,网上自行下载注册机. 调试一定要对应自己板子的晶振,否则当你测试你的延时实际时间时,keil里的sec会不一样,甚至离谱.

  4. 升级版本后,报错go: -i flag is deprecated

    环境 go version go1.18.6 升级go版本后,本地项目起不来 将-i去掉 应用,在重启,则正常启动

  5. SQL Server创建dblink跨库查询

    dblink是跨库查询的主要手段,在Oracle创建DbLink中已经演示了Oracle中如何创建及使用DbLink,这篇博客看看SQL Server中如何使用. 一.通过图形化界面直接创建 选择当前 ...

  6. hdu 1516 String Distance and Transform Process

    Problem DescriptionString Distance is a non-negative integer that measures the distance between two ...

  7. Python学习笔记(六)循环

    一.while循环 1.while在给定的判断条件为True时执行循环体,否则退出循环体 1 flag = True 2 while flag: 3 print('正确') 4 flag = Fals ...

  8. springboot启动报错 Failed to scan *****/derbyLocale_ja_JP.jar from classloader hierarchy

    springboot启动报错 Failed to scan *****/derbyLocale_ja_JP.jar from classloader hierarchy   这两天自己在玩虚拟机,想把 ...

  9. Autojs页面开发

    概述: Autojs功能很强大!  可以打包成apk文件装在手机里运行,也可以做页面UI级别的开发.本文主要对基础页面开发常用方法.demo.资料做收集, 属于比较基础的文章.大佬请略过....... ...

  10. nginx,git,maven面试题

    1.简述一下什么是Nginx,它有什么优势和功能? Nginx是一个web服务器和方向代理服务器,用于HTTP.HTTPS.SMTP.POP3和IMAP协议.因 它的稳定性.丰富的功能集.示例配置文件 ...