0.6.9 • Published 4 years ago

dfv v0.6.9

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

dfv

基于node.js与TypeScript的MVC框架。

同时支持Express与koa。

想要像c#.net mvc,LINQ一样做到从controller层到model与view层完全无硬编码,就只有使用TypeScript之类的强类型语言,并且可以获得强大的智能感知,大大提高开发效率与可维护性。这是原生js所无法实现的。

快速入门:

安装:

npm install dfv

本框架支持与原有的Express或Koa代码共存。

先新建一个简单的controller类:

import {route} from "dfv";
import {valid} from "dfv/src/public/valid";

export class HomeController {

    /**
     * url为:/home/index
     * get请求,并接收一个id参数(会自动转换为number类型)
     */
    @route.get()
    async index(id: number) {
        //返回值作为response body
        return "index:" + id;
    }

    /**
     * /home/api
     * post请求,并且name参数不能为空
     */
    @route.post()
    async api(@valid.stringNotEmpty() name: string) {
        return "api:" + name;
    }

}

如上所示,和C#的特性(attribute),或者java的注解(annotation)一样。TypeScript使用装饰器(decorator),来描述接口的详细信息。只有装饰了@route.get或post的成员函数才能被外部访问。

加载上面的controller类:

import {route} from "dfv";
import * as express from 'express';
import * as path from "path";

//express实例
var app = express();


/**
 * 加载controllers目录里的所有文件
 */
route.load(app, [{
  	
    //controllers目录
    menu: path.join(__dirname, 'controllers'),
  
    //拦截Controller中的每一次URL请求
    onRoute: async (dat) => {
        try {
            if (!dat.valid.ok) {
                //参数校验失败后的行为
                dat.ctx.status = 500;
                dat.ctx.body = dat.valid.msg;
                return;
            }
            //next()即为调用controller类的成员函数
            let ret = await dat.router.next(dat);
            if (ret != null)
                dat.ctx.body = ret;
        } catch (e) {
            console.error(e)
            dat.ctx.status = 500;
            dat.ctx.body = "server error";
        }
    }
}]);

为了同时兼容koa,express的request与response参数被合并转换成了类似koa的context。

当使用koa时需要node v7.6.0以上版本。

而使用Express只需要node v6.9.0以上版本。

性能测试:

单进程下,node v8.1.0版本与原生Express的TPS(每秒响应数)对比:

npm.io

编译到es2017版比es2016高了近1000TPS,node v8.1.0对async与await的优化还是不错的。

koa2版本TPS对比:

npm.io


配置好Express与TypeScript以及各目录结构的demo:

https://github.com/rxaa/dfv-demo

详细教程


1. 入口与目录结构

2. 控制器

3. 数据库ORM

4. tsx模板

0.6.7

4 years ago

0.6.9

4 years ago

0.6.8

4 years ago

0.6.6

4 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.1

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.9

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago