Polix
Node.js Web Framework
polix
是基于koa v2.5.0
的装饰器、插件式开发框架,和平常的Node.js Web Framework
相比,它无需另外绑定路由集合、可拓展、开发简单,依照java
的著名依赖注入框架spring
来制作,让开发者专注于逻辑。polix
采用多服务多进程架构来保证服务的稳定和快速响应能力。polix
的中间件和koa v2.x
的中间件保持兼容。polix
提供Dockerfile
+docker-compose.yml
方案进行部署,默认使用的ORM
是sequelize
(后续会提供polix-orm
)。开发者可以选择ES6/7/8 或者 TypeScript来进行开发。
以上部分功能尚在开发阶段,敬请关注!
Install
$ npm i polix --save
Getting Started
使用
polix-cli
初始化应用
$ npm i polix-cli -g$ pol init example$ cd example$ make build$ make run-dev
Service
在
service
文件夹下添加user.js
const Service = ; { super; this_name = {}; } async { this_nameuserId = name; return this; } async { return this_nameuserId; } moduleexports = UserService;
Controller
在
controller
文件夹下添加user.js
const Controller GET POST DEL PUT = ; // POST /user/addUser @POST async { const body = param; await thisserviceuser; ctxbody = result: 'ok' ; } // GET /user/getUser @GET async { const query = param; let user = await thisserviceuser; ctxbody = user ; } // GET /user/info @ async { ctxbody = v: 'v1.0' } // PUT /user/updateUser @PUT async { ctxbody = status: true } // DEL /user/delUser @DEL async { ctxbody = status: true ; } // GET /user/status/:userId @ async { const router = param; ctxbody = status: true userId: routeruserId ; } moduleexports = UserController;
Middware
polix
的中间件与koa 2.x 的中间件保持兼容
框架默认加载koa-body
中间件,如需另外添加中间件则新建middware
文件夹(与controller
文件夹平级)
添加跨域中间件 ,新建cors.js
:
# corsjs const cors = ;module{ return ;}
该文件夹下必须存在index.js
文件作为总输出中间件文件,加载时根据导出对象的顺序进行绑定中间件
# indexjs const cors = ; moduleexports = cors // 必须是函数 ,绑定方式为:app.use(cors())
Plugin
$ npm i --save polix-request
在项目根目录下的
config
文件夹下的plugin.default.js
中添加以下代码
// `curl`最终会挂载到`this.app`下exportscurl = // 表示是否启用该插件 enable: true // 插件`npm`包名 package: 'polix-request';
在
controller
里用polix-request
@GET async { let result = await thisappcurl; ctxbody = data: result }
polix
已经内置polix-request
插件了,这里只是个演示
Start
$ make dev
Author
Polix © Ricky 泽阳, Released under the MIT License.