{"id":25597958,"url":"https://github.com/k1995/mysrv","last_synced_at":"2025-04-13T03:41:31.908Z","repository":{"id":57306986,"uuid":"82883937","full_name":"k1995/mysrv","owner":"k1995","description":"Yet another Node.js web framework, based on koa.js  又一个 Node.js MVC 框架，基于Koa2","archived":false,"fork":false,"pushed_at":"2017-05-12T16:19:04.000Z","size":41,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-22T21:05:55.486Z","etag":null,"topics":["koa","koa2","mvc","webframework"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k1995.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-23T04:24:28.000Z","updated_at":"2024-07-14T07:41:23.000Z","dependencies_parsed_at":"2022-09-08T05:10:44.109Z","dependency_job_id":null,"html_url":"https://github.com/k1995/mysrv","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/k1995%2Fmysrv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1995%2Fmysrv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1995%2Fmysrv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k1995%2Fmysrv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k1995","download_url":"https://codeload.github.com/k1995/mysrv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248660845,"owners_count":21141357,"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","mvc","webframework"],"created_at":"2025-02-21T13:19:52.235Z","updated_at":"2025-04-13T03:41:31.889Z","avatar_url":"https://github.com/k1995.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MySrv\n\nMysrv shorthand for \"My Server\", it's a MVC Framework for Node.js. Mysrv aims to make it more easily to build enterprise-grade Node.js web.\n\n\n\u003e This is alpha software; use at your own risk\n\n[【中文介绍】](README_zh.md)\n\n## Feature\n\n* Modular programming  \n\n  Cutting up pages into pieces of modules, each module has a corresponding `action`, not just `inculde ` template\n\n* Routing  \n\n  supprot Express-style routing\n\n* Render  \n\n  Using mozilla's `Nunjucks` as the default template engine\n\n* Flexible  \n\n  support custom `koa middleware` \u0026 plugins\n\n\n## Installation\n\n```\nnpm install mysrv\n```\n\n\n\n## Project structure\n```\n--assets/\n\t-css/\n\t-js/\n\t-favicon.ico\n--components/\n\t-contollers/\n\t-middlewares/\n\t-models/\n\t-plugins/\n\t-services/\n--tasks/\n--config/\n--views/\n--app.js\n```\n\n\n\n## Overview\n\n__Controller__\n\n```\nclass IndexController {\n\n    /**\n     * default welcome page\n     * Index as the default action\n     */\n    index() {\n\n        this.render({title: 'Hello'}); //渲染模版，第一个参数传递的数据，可以在模版直接使用\n    }\n\n    /**\n     * convenient action annotation\n     * 方便的action注解指令\n     * 下面action映射的URL为 '/user/login'，并且只允许POST请求\n     */\n    async login($method = 'POST', $url='/user/login') {\n    \t\n    \t// using async/await\n    \tconst resultModel = await this.userService(this.params.userName, this.params.pass);\n       \t\n       \tif(resultModel.success) {\n          ...\n       \t}else{\n          ...\n       \t}\n       \t\n       \tthis.render(null, {layout: 'login'}); //使用名称为 \"login\" 的布局文件\n    }\n    \n    /**\n    * Output JSON\n    */\n    api() {\n    \n      this.json({\n      \tdata: 'Hello Mysrv'\n      });\n    }\n}\n\nmodule.exports = IndexController;\n```\n\n__Render view__\n\n在`action`中调用 `this.render()` 开始模版渲染流程。如果在模版中调用 `{% render \"index:child %}` 会渲染子模版，并返回渲染后的HTML。注意`{% render \"index:child %}`不仅仅只是将其渲染后的HTML包含进来，它会执行完整的 action !\n\nview.html\n\n```html\n\u003cdiv\u003e\n\t{% render \"index:child %}\n\u003c/div\u003e\n```\n\n\n\n上面完成了`view.html`模版的渲染后，还不能直接返回给浏览器，他不是完整的HTML。下面还要渲染 `Layout` 布局模版，`{{ view}}` 会替换为上面的 `view.html`渲染后的内容，这样 `view.html` 和 `layout.html`拼接起来才是一个完整的HTML 页面。 \n\nlayout.html\n\n```html\n\u003chead\u003e\n    \u003ctitle\u003e{{ title }}\u003c/title\u003e\n    ...\n\u003c/head\u003e\n\u003cbody\u003e\n    {% render \"layout:header\" %}\n\n    \u003cdiv class=\"container\"\u003e\n    \n        {{ $view }}\n    \n    \u003c/div\u003e\n    \n    {% render \"layout:footer\" %}\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n\u003e __布局模版__ 顾名思义，即定义一个HTML的基本结构。如一个网站的头部、尾部每个页面基本相同，只是中间内容在发生变化，这些公用的部分就应该放在__布局模版__中，以减少重复代码，主模版就只是中间变化的这部分内容。\n\u003e\n\u003e 布局模版默认是开启的，如果你的主模版(如上面的view.html)已经包含完整的HTML，不想拼接布局模版，通过设置`this.render` 第二个参数 `layout = null` 来关闭布局模式。即 `this.render(data, {layout: null})`。\n\n\n\n## Examples\n\nTo view the examples, clone the Mysrv and install the dependencies\n\n```\n$ git clone https://github.com/k1995/mysrv.git\n$ cd mysrv\n$ npm install\n```\n\nThen run whichever example you want\n\n```\n$ cd examples\n$ node hello\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk1995%2Fmysrv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk1995%2Fmysrv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk1995%2Fmysrv/lists"}