npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dfv

v0.6.9

Published

web mvc framework

Downloads

87

Readme

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

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

koa2版本TPS对比:


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

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

详细教程


1. 入口与目录结构

2. 控制器

3. 数据库ORM

4. tsx模板