框架结构例子

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. JavaSE——==与.equal()方法

    ==号的作用 比较基本数据类型:比较的是具体的值 比较引用数据类型:比较的是对象地址值 package com.zhao.stringdemo; public class StringDemo2 { ...

  2. 关于certutil的探究-文件下载+编码分块上传上传文件再合并

    何为certutil certutil.exe 是一个合法Windows文件,用于管理Windows证书的程序. 微软官方是这样对它解释的: Certutil.exe是一个命令行程序,作为证书服务的一 ...

  3. Delphi模拟win+tab按键效果

    //按下左侧win键 keybd_event(VK_LWIN,0,0,0); //按下tab键 keybd_event(VK_TAB,0,0,0); //抬起左侧win键 keybd_event(VK ...

  4. @Async 注解的使用

    1.@Async介绍 在Spring中,基于@Async标注的方法,称之为异步方法:这些方法将在执行的时候,将会在独立的线程中被执行,调用者无需等待它的完成,即可继续其他的操作 例如, 在某个调用中, ...

  5. (已解决)MYSQL怎么实现表的id在插入删除的前提下连续递增?

    其实就是用了sql语句. 思路是重置所有id和auto_increment. SET @i=0; UPDATE records SET id=(@i:=@i+1); ALTER TABLE recor ...

  6. pycharm、pyqt5、pyuic、anaconda配置界面

    转载一篇很棒的文档,讲解的是如何在pycharm里面使用QT  desiger勾画界面并且将相应的界面转化成py文件 https://www.jianshu.com/p/8b992e47a0e4 个人 ...

  7. 7.29关灯游戏,用script实现

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  8. Linux_CMD_FOR_FILE&FOLDER

    0,删除文件夹和子文件夹:rm -rf /xxx/xxx 0,拷贝整个文件夹:cp -r /xxx/xxx /ddd/ 0,改文件名:mv oldName newName 0,新建文件:touch f ...

  9. 日常遇到bug小记

    archery查询后显示缺少空格: https://github.com/hhyo/Archery/pull/1449 archery-mongo数据库查询skip不生效: https://githu ...

  10. SPI主机Verilog代码实现

    前面已经提到过了SPI,在SPI从机的设计中已经讲过SPI的基本原理,这里就不再赘述.对于SPI的主机可以参考百度百科或则笔者前面写的SPI从机介绍的相关知识. 下面是SPI_master的代码 SP ...