zenweb
v5.1.2
Published
Modular lightweight web framework based on Koa
Downloads
1,035
Maintainers
Readme
ZenWeb
Modular lightweight web framework based on Koa
Document
Install
# for production
npm install zenweb
# for development
npm install dotenv typescript rimraf tsc-watch --save-dev
Project Code
edit package.json
file at scripts
:
"scripts": {
"start": "node --enable-source-maps app",
"dev": "rimraf app && tsc-watch --onSuccess \"npm run dev-start\"",
"dev-start": "node -r dotenv/config --enable-source-maps app",
"build": "rimraf app && tsc"
}
create tsconfig.json
file
{
"extends": "zenweb/tsconfig-app",
"compilerOptions": {
"outDir": "./app"
},
"include": ["src/**/*"]
}
create .env
file
APP_NAME=myweb
NODE_ENV=development
DEBUG=*
create src/index.ts
file
import { create } from 'zenweb';
create().start();
create src/service/hello.ts
file
import { inject } from 'zenweb';
export class HelloService {
@inject ctx!: Context;
getIp() {
return this.ctx.ip;
}
}
create src/controller/hello.ts
file
import { mapping } from 'zenweb';
import { HelloService } from '../service/hello';
export class HelloController {
@mapping()
index(service: HelloService) {
const ip = service.getIp();
return `Hello ZenWeb! ${ip}`;
}
}
start server:
npm run dev
内置模块
- core 核心
- meta 运行基本信息,例如:请求耗时
- inject 注入支持
- router 路由支持
- log 日志支持
- result 统一结果返回,成功或失败
- messagecode 统一错误消息格式化
- 依赖 inject, result
- controller 类控制器支持
- 依赖 inject, router
- helper 输入数据验证助手
- 依赖 inject, messagecode
- body 请求主体解析,JSON、Form
- 依赖 inject, helper
内置模块默认开启,可以通过设置配置项为 false 关闭