An open API service indexing awesome lists of open source software.

https://github.com/k1995/mysrv

Yet another Node.js web framework, based on koa.js 又一个 Node.js MVC 框架,基于Koa2
https://github.com/k1995/mysrv

koa koa2 mvc webframework

Last synced: 20 days ago
JSON representation

Yet another Node.js web framework, based on koa.js 又一个 Node.js MVC 框架,基于Koa2

Awesome Lists containing this project

README

        

# MySrv

Mysrv 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.

> This is alpha software; use at your own risk

[【中文介绍】](README_zh.md)

## Feature

* Modular programming

Cutting up pages into pieces of modules, each module has a corresponding `action`, not just `inculde ` template

* Routing

supprot Express-style routing

* Render

Using mozilla's `Nunjucks` as the default template engine

* Flexible

support custom `koa middleware` & plugins

## Installation

```
npm install mysrv
```

## Project structure
```
--assets/
-css/
-js/
-favicon.ico
--components/
-contollers/
-middlewares/
-models/
-plugins/
-services/
--tasks/
--config/
--views/
--app.js
```

## Overview

__Controller__

```
class IndexController {

/**
* default welcome page
* Index as the default action
*/
index() {

this.render({title: 'Hello'}); //渲染模版,第一个参数传递的数据,可以在模版直接使用
}

/**
* convenient action annotation
* 方便的action注解指令
* 下面action映射的URL为 '/user/login',并且只允许POST请求
*/
async login($method = 'POST', $url='/user/login') {

// using async/await
const resultModel = await this.userService(this.params.userName, this.params.pass);

if(resultModel.success) {
...
}else{
...
}

this.render(null, {layout: 'login'}); //使用名称为 "login" 的布局文件
}

/**
* Output JSON
*/
api() {

this.json({
data: 'Hello Mysrv'
});
}
}

module.exports = IndexController;
```

__Render view__

在`action`中调用 `this.render()` 开始模版渲染流程。如果在模版中调用 `{% render "index:child %}` 会渲染子模版,并返回渲染后的HTML。注意`{% render "index:child %}`不仅仅只是将其渲染后的HTML包含进来,它会执行完整的 action !

view.html

```html


{% render "index:child %}

```

上面完成了`view.html`模版的渲染后,还不能直接返回给浏览器,他不是完整的HTML。下面还要渲染 `Layout` 布局模版,`{{ view}}` 会替换为上面的 `view.html`渲染后的内容,这样 `view.html` 和 `layout.html`拼接起来才是一个完整的HTML 页面。

layout.html

```html

{{ title }}
...

{% render "layout:header" %}



{{ $view }}



{% render "layout:footer" %}