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

@mybricks/rocker-mvc

v0.0.2

Published

MVC Framework for typescript, powered by rocker.

Downloads

4

Readme

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()