Annotatify
特性
- 基于express框架,可以快速入门;
- 多项目部署、项目分层管理;
- 使用
annotation
注释模式的路由; - 使用
annotation
注入service
(中间件); - 使用swig模板作为默认模板引擎;
安装使用
使用npm安装
npm install annotatify
使用 yarn 安装
yarn add mvcify
快速开始
项目默认目录结构如下:
project
|
|- apps # 应用目录
| |- front-end # 网站前端
| | |- routes # 控制器
| | | |- index.js # - index路由
| | | |- user.js # - user路由
| | | `- ...
| | |- services # 中间件目录
| | |- assets # 静态资源目录
| | |- views # 视图目录
| | |- config.js # 配置
| | `- ...
| `- ...
|- assets
|- node_modules
|- services
|- config.js
|- app.js
|- package.json
`- ...
应用入口文件
四行代码轻松启动你的web应用。
// app.js
const {Annotatify} = require('../lib/index');
const app = new Annotatify(__dirname);
app.setup({'front-end': '/'});
app.express.listen(8080);
应用于路由类上的annotation
程序定义了一些列的指令,作用于控制器的annotation内,轻松实现路由注册,这样 方便开发、路由与方法绑定及查询,具体指令有:
- get('/path')
- post('/path')
- put('/path')
- patch('/path')
- delete('/path')
- head('/path')
- options('/path')
- route(["method1","method2"], path="/path")
另外,annotation还支持使用下列指令注册中间件:
- before("services目录下的中间件文件名")
- after("services目录下的中间件文件名")
可以多次调用,表示注册多个service 值得注意的是:前一个指令依赖后一个指令是否调用next方法
HTTP方法指令优先级高于route指令,也就是说如果存在HTTP方法指令,则route中对应的的方法无效
// index.js/** * 这里的route时必须的 * * @route("/"); * @before("serviceName"); * @before("serviceName2"); * @after("serviceName") */ { thisapp = app; } /** * 通过before指令注册的service会在该方法前被执行; * 该方法执行了next,才会执行通过after注册的service * * @get("/"); * @before("serviceName"); * @after("serviceName") */ { console; res; } /** * route指令中由GET、POST、DELETE、UEF四个方法,其中UEF是不被支持的; * 另外,又使用了 get、post指令,route中的GET、POST方法则无效了。 * * @route(["GET", 'post', "Delete", 'UEF']); * @get("/oo"); * @post("/ii"); */ { res; } moduleexports = IndexRouter;