# dfv

> web mvc framework

Latest version **0.6.9** (published 2020-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install dfv
pnpm add dfv
yarn add dfv
bun add dfv
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.9 |
| Published | 2020-09-05 |
| First published | 2017-06-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8.0.0 |
| Dependencies | 11 |
| Unpacked size | 984 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | rxaa |
| Maintainers | pqe |
| Keywords | web, mvc, dfv, express, koa, typescript |

## Links

- npm: https://www.npmjs.com/package/dfv
- Repository: https://github.com/rxaa/dfv
- Homepage: https://github.com/rxaa/dfv#readme
- Issues: https://github.com/rxaa/dfv/issues
- npm.io page: https://npm.io/package/dfv

## Dependencies (11)

- [mysql](https://npm.io/package/mysql.md) ^2.18.1
- [mongodb](https://npm.io/package/mongodb.md) ^3.6.1
- [formidable](https://npm.io/package/formidable.md) ^1.2.2
- [koa-router](https://npm.io/package/koa-router.md) ^9.4.0
- [@types/node](https://npm.io/package/@types/node.md) ^14.6.4
- [@types/mysql](https://npm.io/package/@types/mysql.md) ^2.15.15
- [@types/mongodb](https://npm.io/package/@types/mongodb.md) ^3.1.31
- [agentkeepalive](https://npm.io/package/agentkeepalive.md) ^4.1.3
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@types/formidable](https://npm.io/package/@types/formidable.md) ^1.0.31
- [source-map-support](https://npm.io/package/source-map-support.md) ^0.5.19

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.6.9 (latest) — 2020-09-05
- 0.6.8 — 2020-09-05
- 0.6.7 — 2020-09-05
- 0.6.6 — 2020-09-04
- 0.6.4 — 2020-09-04
- 0.6.3 — 2020-09-04
- 0.6.2 — 2020-09-04
- 0.6.1 — 2020-09-04
- 0.6.0 — 2020-09-04
- 0.5.2 — 2019-10-18
- 0.5.1 — 2019-06-02
- 0.5.0 — 2019-05-30
- 0.4.1 — 2018-10-31
- 0.3.13 — 2018-06-13
- 0.3.12 — 2018-06-13
- … 26 more at https://npm.io/package/dfv/versions

## README

# dfv

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

同时支持Express与koa。

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

# 快速入门：

**安装：**

```shell
npm install dfv
```

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

**先新建一个简单的controller类:**

```typescript
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类：**

```typescript
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(每秒响应数)对比：

![](/doc/performance.png)

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

**koa2版本TPS对比：**

![](/doc/performance_koa.png)

------

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

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

# 详细教程

------

[1. 入口与目录结构](doc/1.start.md)

[2. 控制器](doc/2.controller.md)

[3.  数据库ORM](doc/3.orm.md)

[4. tsx模板](doc/4.tsx.md)

---
_Source: https://npm.io/package/dfv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
