All Projects → k1995 → Mysrv

k1995 / Mysrv

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Mysrv

polix
🚀 Node.js Web Framework
Stars: ✭ 32 (+220%)
Mutual labels:  koa, webframework, koa2
Sactive Web
🚀 A dependency injection web framework for Node.js.
Stars: ✭ 143 (+1330%)
Mutual labels:  koa, koa2, webframework
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+8770%)
Mutual labels:  koa, koa2
Koa2 Note
《Koa2进阶学习笔记》已完结🎄🎄🎄
Stars: ✭ 4,725 (+47150%)
Mutual labels:  koa, koa2
Koa Helmet
Important security headers for koa
Stars: ✭ 595 (+5850%)
Mutual labels:  koa, koa2
Nodeclub Koa
use koa to rewrite nodeclub
Stars: ✭ 18 (+80%)
Mutual labels:  koa, koa2
Koa Rest Api Boilerplate
💯 Boilerplate for Node.js Koa RESTful API application with Docker, Swagger, Jest, CodeCov and CircleCI
Stars: ✭ 420 (+4100%)
Mutual labels:  koa, koa2
Koa Weixin Jssdk
koa weixin jssdk middleware
Stars: ✭ 7 (-30%)
Mutual labels:  koa, koa2
Plover
专注于模块化的NodeJs Web框架
Stars: ✭ 259 (+2490%)
Mutual labels:  koa, webframework
Cykspace Node
博客后台服务~~ 👉👉 http://www.cykspace.com
Stars: ✭ 23 (+130%)
Mutual labels:  koa, koa2
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (+6840%)
Mutual labels:  koa, koa2
Vue Koa Demo
🔰A simple full stack demo(CSR & SSR & Docker Support) written by Vue2 & Koa2(Koa1 verson also completed)
Stars: ✭ 730 (+7200%)
Mutual labels:  koa, koa2
Awesome Koa
👯 Awesome Koa.js Web Framework
Stars: ✭ 343 (+3330%)
Mutual labels:  koa, koa2
Egg
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
Stars: ✭ 17,616 (+176060%)
Mutual labels:  koa, koa2
Koa Generator
Koa' application generator for 1.x and 2.x( Express-style and support all middlewares include async/await )
Stars: ✭ 929 (+9190%)
Mutual labels:  koa, koa2
Koahub
KoaHub.js -- 中文最佳实践Node.js Web快速开发框架。支持Koa.js, Express.js中间件。当前项目已停止维护,推荐使用Doodoo.js
Stars: ✭ 308 (+2980%)
Mutual labels:  koa, koa2
Koajs Design Note
《Koa.js 设计模式-学习笔记》已完结 😆
Stars: ✭ 520 (+5100%)
Mutual labels:  koa, koa2
Koa Passport
Passport middleware for Koa
Stars: ✭ 748 (+7380%)
Mutual labels:  koa, koa2
koa-router-version
Semantic Versioning routing for Koa
Stars: ✭ 19 (+90%)
Mutual labels:  koa, koa2
koa2-winston
koa2 version winston logger like express-winston
Stars: ✭ 37 (+270%)
Mutual labels:  koa, koa2

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

【中文介绍】

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

<div>
	{% render "index:child %}
</div>

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

layout.html

<head>
    <title>{{ title }}</title>
    ...
</head>
<body>
    {% render "layout:header" %}

    <div class="container">
    
        {{ $view }}
    
    </div>
    
    {% render "layout:footer" %}
</body>
</html>

布局模版 顾名思义,即定义一个HTML的基本结构。如一个网站的头部、尾部每个页面基本相同,只是中间内容在发生变化,这些公用的部分就应该放在__布局模版__中,以减少重复代码,主模版就只是中间变化的这部分内容。

布局模版默认是开启的,如果你的主模版(如上面的view.html)已经包含完整的HTML,不想拼接布局模版,通过设置this.render 第二个参数 layout = null 来关闭布局模式。即 this.render(data, {layout: null})

Examples

To view the examples, clone the Mysrv and install the dependencies

$ git clone https://github.com/k1995/mysrv.git
$ cd mysrv
$ npm install

Then run whichever example you want

$ cd examples
$ node hello
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].