0.0.2 • Published 3 years ago

@mybricks/rocker-mvc v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

rocker-mvc

面向企业开发级的Web应用框架

安装

npm install rocker-mvc --save

Node.js >= 8.0.0 required.

特性

✔︎ 简单易用,符合工程师直觉 ✔︎ 一体化的开发集成框架 ✔︎ 面向对象风格的编程开发体验 ✔︎ 灵活的插件扩展

场景.文档

(以下代码样例均由Typescript实现) 基本路由使用 重定向 作为静态资源服务器使用 文件下载 统一错误处理

TraceLocal(模拟LocalThread))

基本路由使用

rocker-mvc/samples/router └src └router ├router.ts └tpt.ejs └start.ts └package.json

start.ts

import {route} from "rocker-mvc";

/**
 * 配置路由并启动
 */
route({
    '/sample': require('./src/demo/router')
})
    .start()//启动(默认端口8080)

src/demo/router.ts

import {Get, Param, Request} from "rocker-mvc";

//在start.ts 中配置了路由规则为
// '/demo': require('./src/homepage/router')

export default class {
    /**
     * 【样例1】 匹配Get(/demo)
     */
    @Get({url: '/', render: './tpt.ejs'})//@Get注释该方法表明为Get方法,url:匹配的路径,render:返回的摸版路径
    async get0(@Param('id') _id: string, @Request _ctx) {//此处的方法名可以是任意的,通过 @Param描述对应请求中的参数
        //返回给摸版引擎的数据对象
        return {message: `The input param id value is: ${_id}`};
    }

    /**
     * 【样例2】 匹配 Get(/demo/test)
     */
    @Get({url: '/test', render: './tpt.ejs'})
    async get1(@Param({name: 'id', required: true}) _id: string, @Param('age') _age: number) {//@Param描述了一个“非空”的id及可为空的age参数
        //返回给摸版引擎的数据对象
        return {message: `The input param id value is: ${_id}`};
    }

    /**
     * 匹配 Post(/demo)
     */
    @Post({url: '/'})
    async post(@Param('id') _id: string) {
        //直接将json返回
        return {message: `The input param id value is: ${_id}`};
    }


    /**
     * 同时匹配 Get Post(/demo)
     */
    @Get Post({url: '/both'})
    async post(@Param('id') _id: string) {
        //直接将json返回
        return {message: `The input param id value is: ${_id}`};
    }
}

src/demo/tpt.ejs

<div>
    <div><%=message%></div>
</div>

重定向

src/demo/router.ts

    /**
     * Redirect
     * @returns {Promise<void>}
     */
    @Get({url:'/redirect'})
    async redirect(){
        let rp:RedirectResp = new RedirectResp('/test/base');
        rp.code = 301;
        return rp;
    }

作为静态资源服务器使用

rocker-mvc/samples/assets └src └assets └assets └test.js └start.ts └package.json

start.ts

import {route} from "rocker-mvc";
import * as path from 'path';

/**
 * 作为静态服务器使用
 */
let assetsPath = path.join(path.resolve('./'), './src/assets/');
route({assetsPath}).start();
// 1. assetsPath选项指定静态文件内容的位置
// 2. 静态内容处理优先级低于router配置
// 3.rocker-mvc提供了不允许访问静态资源之外内容的安全机制

文件下载

start.ts

    @Get({url:'/download'})
    async download(){
        let fileName = 'somefile.zip';
        const fileStream = fs.createReadStream(require('path').join(__dirname,'../../',fileName));
        return new DownloadResp(fileName,fileStream);
    }

统一错误处理

start.ts

route({
    errorProcessor:error=>{//ex为拦截到的错误对象
        console.log(error);
        return {code:-1,tid}
    }
})({
    '/test':require('./src/router/router')
}).start();

Tracelocal(模拟Threadlocal)

需要依赖 commons,rocker-mvc针对其接口做了实现,Tracelocal的API

    get id(): string

    set(key: string, value: any): void

    get(key: string): any

例如,在全局错误处理中打印traceId start.ts

import {Tracelocal} from 'commons'

route({
    errorProcessor:ex=>{
        let traceId = Tracelocal.id;
        console.log(`当前Trace Id ${traceId}`)
        return {code:-1,traceId}
    }
})({
    '/test':require('./src/router/router')
}).start()