{"id":14989454,"url":"https://github.com/luwnto/koa-init","last_synced_at":"2025-04-12T01:17:01.750Z","repository":{"id":35579829,"uuid":"207992244","full_name":"Luwnto/koa-init","owner":"Luwnto","description":"简单封装的开箱即用的koa框架","archived":false,"fork":false,"pushed_at":"2022-12-06T14:45:37.000Z","size":452,"stargazers_count":11,"open_issues_count":8,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T01:16:52.865Z","etag":null,"topics":["koa","koa2","koajs","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Luwnto.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-12T07:37:20.000Z","updated_at":"2022-05-08T00:56:23.000Z","dependencies_parsed_at":"2023-01-16T00:15:58.303Z","dependency_job_id":null,"html_url":"https://github.com/Luwnto/koa-init","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Luwnto%2Fkoa-init","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Luwnto%2Fkoa-init/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Luwnto%2Fkoa-init/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Luwnto%2Fkoa-init/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Luwnto","download_url":"https://codeload.github.com/Luwnto/koa-init/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248501859,"owners_count":21114684,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["koa","koa2","koajs","nodejs"],"created_at":"2024-09-24T14:18:23.892Z","updated_at":"2025-04-12T01:17:01.721Z","avatar_url":"https://github.com/Luwnto.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## 写在前面\n\n![](https://img.shields.io/badge/node-%3E%3D8.1.0-green) ![](https://img.shields.io/badge/mysql-%3E%3D5.7.0-red) ![](https://img.shields.io/badge/npm-%3E%3D6.0.0-brightgreen)\n\n找了很久没找到合适的开箱即用的Koa框架, 自己动手造了一个, 封装了很多实用的功能, 集成了Mysql的CURD, 完成了一个登陆和注册的功能, 欢迎使用\n\n## 开始使用\n\n### 安装\n\n从Git仓库下载或克隆项目到本地:\n```\n$ git clone git@github.com:Luwnto/koa-init.git\n```\n\n进入到项目目录, 复制 `env.example.js` 到项目根目录, 名字改为 `env.js`:\n```\n$ cp env.example.js env.js\n```\n\n修改env.js, 将数据库配置改成你的:\n```\n$ vim env.js\n```\n\n安装依赖:\n```\n$ npm install\n```\n\n执行数据迁移, 完成表创建:\n```\n$ npm run migrate\n```\n\n运行项目:\n```\n$ npm run start\n```\n\n浏览器中访问项目:\n```\nhttp://127.0.0.1:3000\n```\n\n### 基本命令\n\n如果你想更加方便的开发项目, 需要安装 `nodemon`:\n```\n$ sudo npm install -g nodemon\n```\n\n测试环境启动:\n```\n$ npm run start\n```\n\n生产环境启动, 同时开启守护进程:\n```\n$ npm run build\n```\n\n测试环境启动, 同时开启守护进程(推荐开发使用):\n```\n$ npm run dev\n```\n\n数据迁移(重新创建表):\n```\n$ npm run migrate\n```\n\n\u003e 测试环境会显示报错信息, 生产环境会显示404页面, 生产环境还会生成页面缓存\n\n### 数据库模型和数据迁移\n\n数据库模型文件写在 `modules` 目录下, 使用ORM操作数据库的时候引入模型文件即可\n\n数据库模型文件代码示例:\n```\nconst db = require('../utils/db');\n\nmodule.exports = db.defineModel('users', {\n    username: { // 账号\n        type: db.STRING(100),\n        unique: true\n    },\n    password: db.STRING(100), // 密码\n    gender: db.BOOLEAN, // 性别\n    avatar: db.STRING(100) // 头像\n});\n```\n\n创建好数据库模型文件之后, 执行迁移命令即可完成表创建:\n```\n$ npm run migrate\n```\n\n\u003e 注意迁移命令会清空所有表, 并重新创建, 生产环境禁用\n\n### 路由\n\n在项目启动的时候会自动扫描指定目录下的路由文件, 修改 `app.js` 的配置即可:\n```\n// 注册路由控制器\napp.use(controller(['/controllers', '/controllers/home', '/controllers/admin']));\n```\n\n路由和业务代码默认放在 `controllers` 目录下, 代码示例如下:\n```\nvar index = async (ctx, next) =\u003e {\n    ctx.render('home/index.html');\n};\n\nvar error = async (ctx, next) =\u003e {\n    ctx.render('404.html');\n};\n\nmodule.exports = {\n    'GET /': index,\n    'GET /404': error,\n};\n```\n\n### 模板渲染\n\n使用 [Nunjucks](http://mozilla.github.io/nunjucks/) 模板引擎, 详细用户可以参考手册\n\nfor循环:\n```\n{% for t in test %}\n    {{ t.name }}\n{% endfor %}\n```\n\nif判断:\n```\n{% if test.name == 'li' %}\n\t  hello\n{% else %}\n\t  world\n{% endif %} \n```\n\n### 全局变量\n\n封装了部分变量到全局变量, 可以在模板引擎中直接使用:\n```\n\u003cspan\u003e{{ session.user.name }}\u003c/span\u003e\n```\n\n你可以使用这些全局变量:\n\n* session session相关的数据\n* body post请求相关的数据\n* query get请求query查询相关的数据\n* path 请求的路径, 如果 /user\n\n你也可以在 `utils/templating.js` 文件中自行添加你需要的全局变量:\n```\n// 添加session到全局\nenv.addGlobal('session', ctx.session);\n```\n\n### 数据库增删改查\n\n添加一条数据:\n```\n// 引入模型文件\nconst User = require('../../models/User');\n\n// 插入数据库一条数据\nvar user = await User.create({\n    username: username,\n    password: crypto(password),\n    gender: 1,\n    avatar: '/public/img/avatar.png',\n});\nconsole.log(user);\n```\n\n查询一条数据:\n```\n// 引入模型文件\nconst User = require('../../models/User');\n\n// 查询一条数据\nvar user = await User.find({\n    where: {\n        username: username\n    }\n});\nconsole.log(user);\n```\n\n查询多条数据:\n```\n// 引入模型文件\nconst User = require('../../models/User');\n\n// 查询一条数据\nvar users = await User.findAll({\n    where: {\n        username: username\n    }\n});\nconsole.log(users);\n```\n\n更新一条数据:\n```\n// 引入模型文件\nconst User = require('../../models/User');\n\n// 更新数据, 第一个参数是要更新的数据, 第二个参数是查询条件\nvar user = await User.update({\n    gender: 0,\n}, {'where': {username: 'admin'}});\nconsole.log(user);\n```\n\n删除一条数据:\n```\n// 引入模型文件\nconst User = require('../../models/User');\n\n// 删除数据, 参数是查询条件\nvar user = await User.destroy({'where': {username: 'admin'}});\nconsole.log(user);\n```\n\n## 参考资料\n\n[Koa-router](https://www.npmjs.com/package/koa-router)\n\n[Nunjucks文档](https://nunjucks.bootcss.com/)\n\n[Sequelize文档](https://sequelize.org/master/)\n\n## LICENSE\n\n[MIT](https://github.com/Luwnto/koa-init/blob/master/LICENSE)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluwnto%2Fkoa-init","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluwnto%2Fkoa-init","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluwnto%2Fkoa-init/lists"}