{"id":19916857,"url":"https://github.com/rubylouvre/agate","last_synced_at":"2025-08-04T20:17:03.739Z","repository":{"id":29571752,"uuid":"33111243","full_name":"RubyLouvre/agate","owner":"RubyLouvre","description":"灵活易用的nodejs后端框架","archived":false,"fork":false,"pushed_at":"2017-04-16T23:26:51.000Z","size":810,"stargazers_count":74,"open_issues_count":7,"forks_count":21,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-22T12:35:32.982Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"facebook/react","license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RubyLouvre.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":"2015-03-30T08:23:13.000Z","updated_at":"2024-04-02T17:41:53.000Z","dependencies_parsed_at":"2022-09-04T09:23:54.181Z","dependency_job_id":null,"html_url":"https://github.com/RubyLouvre/agate","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/RubyLouvre%2Fagate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyLouvre%2Fagate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyLouvre%2Fagate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RubyLouvre%2Fagate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RubyLouvre","download_url":"https://codeload.github.com/RubyLouvre/agate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252154732,"owners_count":21702982,"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":[],"created_at":"2024-11-12T21:47:49.796Z","updated_at":"2025-05-03T06:30:37.691Z","avatar_url":"https://github.com/RubyLouvre.png","language":"JavaScript","readme":"agate是去哪儿网酒店前端架构组推出一个nodejs框架，能帮你迅速搭好架子。\n\n主要依赖技术\n__________________________\nkoa + nodemon + log4js + ...\n\n\u003cb\u003e为什么使用koa\u003c/b\u003e  ? nodejs三大框架express, koa, hapi， 目前国内最流行的是前两者。\n\nexpress需要历史悠久,但版本众多,许多模块可能只运行于某一版本中,查起来非常麻烦,并且许多依赖都独立出去, 一盘散沙似的，不方便学习。\n\n\nkoa是基于generator与co之上的新一代的中间件框架, 代表着历史的前进方向。虽然受限于generator的实现程度。但是它的优势却不容小觑。\n\n1. 有了koa，我们可以很好的解决回调的问题。只要yield就行，还可以直接用try来捕获异常\n2. koa会自动帮你改造node的req，res对象，省去你很多工作。再也不需要每个res.end都要写一大堆返回状态了，\n也不需要各种检测错误了，也不需要每次都用finish来确保程序正常关闭了。\n3. 内置了很多以前express的第三方基础库，更加方便。这样你写中间件的时候没必要到处安装依赖库。\n\n目录结构\n```\n│\n├──agate.js\n├──server.js\n├──app\n│　　├──layouts\n│    │    └──layout1.html\n│　　└──pages\n│           ├──home\n│           │     ├──controller.js\n│           │     └──index.html\n│           └──xxx\n│                  ├──controller.js\n│                  └──index.html\n├──config\n│　　 ├──filters.js\n│　　 ├──log4js.js\n│　　 └──routes.json\n├──core\n│　　 └──lur.js\n├──bin\n│　 　└──agate.js\n├──public\n│     └──favicon.ico\n├──logs\n│     └──favicon.ico       \n├──node_modules\n└──package.json\n```  \nhttp://www.veryhuo.com/a/view/39755.html\n\n\n## 启动命令\n```\nagate start 3000 '' prod\nagate start 3000 '' test\nagate start 3000 '' dev\n```\n\n## 脚手架命令\n```javascript\nnode --harmony agate \n```\n\n##直接启动命令\n```javascript\nagate agate scaffold /test2 test2  index post#create\n```\n\n\n在开发环境使用 nodemon, 在生产环境使用pm2\n//http://ourjs.com/detail/52456ae04cd0e14503000009\n\n\n\n\u003cb\u003e为什么使用log4js\u003c/b\u003e  ? 其前身是log4j， 历史悠久， 质量有保证， 并且提供各种日志打印方式及保存方案。\n\n\u003cp\u003e扼要地说, 在app里面写业务代码, 在config里写各种配置,在public里放静态页面。\u003c/p\u003e\n\u003cp\u003e当你下载好本框架后，直接npm install就能安装好各种依赖，\n    然后进入config目录，使用 pm2 start processes.json， 于是服务器就起来了。\u003c/p\u003e\n\u003cp\u003eapp是写业务代码， 里面有两个目录pages与layout， pages下面应该是一个个目录，每个目录代表一个页面。\n   每个目录有一个controller.js（它就是MVC中的C），C里面以这样的形式组织代码：\n\u003c/p\u003e\n```javascript\nexports.index  = function*(next) {\n    yield this.render(\"doc-session/index\",{xxx: \"111\"})\n}\n\nexports.list  = function*(next) {\n    yield this.render(\"doc-session/list\")\n}\n\nexports.create  = function*(next) {\n    yield this.render(\"doc-session/create\")\n}\n\nexports.delete  = function*(next) {\n    yield this.render(\"doc-session/delete\")\n}\n\n```\n\u003cp\u003e这些index, list, create, delete方法就是\u003cb\u003econtroller\u003c/b\u003e中的\u003cb\u003eaction\u003c/b\u003e，一个action应该对应页面上的一个HTTP请求，它用于响应请求返回数据或页面，\n    或者转交其他action进来处理（用术语来说就是重定向）。如果是返回页面，就使用this.render方法，它有两个参数，\n    一个是指定当前的子页面（子页面要结合layout才能变成一个完整的页面），第一个参数是各种数据及这个页面的其他配置。 \u003c/p\u003e\n\u003cp\u003e现在我们有4个action，那么理应在该目录下建4个页面(MVC中的V)，我们现在是使用ejs模块。详见下面模板引擎这一节\u003c/p\u003e\n\u003cp\u003e\n    页面配置已经写app.js中了,没有把握请不要改动它\n\u003c/p\u003e\n```javascript\nvar render = require('koa-ejs');\nrender(app, {\n    root: path.join(__dirname, 'app', \"pages\"), //所有页面模板所在的位置\n    layout: '../layout/template',    //默认所有页面都使用这个layout\n    viewExt: 'html',             \n    cache: app.env !== \"development\" ,//开发环境不进行缓存\n    debug: true\n        // locals: locals,\n        // filters: filters\n});\n```\n\u003cp\u003e如果我们有许多业务逻辑，那么请在对应目录下添加对应JS文件（MVC中的C），然后在action中调用\u003c/p\u003e\n\u003cp\u003e有了MVC了， 那么我们的请求如何才能到达这里呢，那就需要路由系统了。路由系统会根据config下的routes.js定义的路由规则进行定义\n定义到对应的controller下的某action下。一个完整的路由规则如下：\n\u003c/p\u003e\n```javascript\nroutes[\"get /xxxx\"] = {\n    controller: \"xxx\",\n    action: \"index\"\n}\n```\n\n\u003cp\u003e我们写代码时，其操作过程是反过来的，首先是在routes.js下添加路由规则，然后在pages目录下建议你的页面目录，下面建立controller.js\n然后里面有多少个请求，就建议多少个action,然后再建立对应的页面及模型JS， 如果不满意原layout页面，可以再建新的layout，\n然后通过this.render方法的第2个参数指定。\n\u003c/p\u003e\n\u003cp\u003e这样就over了。什么日志， session, cookie, 多线程并发都为你准备好了。\u003c/p\u003e\n\n更多教程，当你启动本工程后，首页就是教程首页。然后你再将routes中的路由规则重设首页，添加你自己的页面！\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubylouvre%2Fagate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frubylouvre%2Fagate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frubylouvre%2Fagate/lists"}