详解IWinter 一个路由转控制器的 Nodejs 库
作者:yvanwangl 时间:2024-05-03 15:56:03
IWinter 是一个路由转控制器的 node 库,只解决一个问题:为了让使用者以更优雅的姿势进行路由的编写。支持在 Express 和 Koa 中使用。
项目地址:https://github.com/yvanwangl/iwinter
简介:
最近在学习使用 Typescript ,打算把以前写的博客管理后台用 Typescript 进行全面重构,在重构服务端代码时接触到使用了 Typescript 的装饰器功能。可以用装饰器对路由进行一层包装,然后就可以这样编写路由:
import {Path, GET, POST, PathParam, BodyParam} from 'iwinter';
@Path('/api/orders')
class OrdersController {
@GET
@Path('/:name/:id', (ctx, next)=> ~~ctx.params.id > 20)
getAllOrders(@PathParam('id') id: number, @PathParam('name') name: string){
return [{
id: id, name, content: 'test', author: 'test', comments: []
}];
}
@POST
@Path('/add')
addPost(@BodyParam('order') order: object){
return order;
}
}
export default OrdersController;
由于工作中会使用用 Java 编写控制层代码(Spring MVC 的控制层代码),所以看到这样的代码简直太亲切了。而且相较于以前的路由编写方式更清晰,更方便维护,所以就将原来的的代码进行了改造。我使用之后发现非常好用,我就想让更多的人都可以使用,所以就诞生了 IWinter 这个库。IWinter 就是将各种装饰器或装饰器工厂函数进行封装然后暴露出一些 API 供大家使用。从此就告别这样编写路由了 ;)
//app.js
...
let users = require('./routes/users');
let orders = require('./routes/orders');
app.use('/api/users', users);
app.use('/api/orders', orders);
...
//routers/orders.js
...
router.route('/')
.get(function (req, res, next) {
let {page, timeRange, customerId, orderNumber} = req.query;
let limit = constants.PAGE_SIZE;
let skip = (page - 1) * limit;
let currentUser = global[Symbol.for('currentUser')];
let queryCondition = {
userId: currentUser['_id']
};
...
谁适合使用 IWinter:
Nodejs 用户
Typescript 用户
Express / Koa 用户
IWinter 安装 及 使用
npm install --save iwinter
import IWinter from 'iwinter';
Koa 中如何使用:
import * as Router from 'koa-router';
...
app.use(new IWinter({
engine: 'koa',
router: new Router(),
dir: path.join(__dirname, 'controller')
}).controller());
...
Express 中如何使用:
import * as express from 'express';
let app = express();
let router = express.Router();
...
new IWinter({
engine: 'express',
router: app,
dir: path.join(__dirname, 'controller'),
prefix: ''
}).controller();
来源:https://juejin.im/post/5a0a5d21f265da431769a75b
标签:Nodejs,路由,转控制器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python的Django框架中实现Hacker News的一些功能
2023-11-24 19:27:46
Oracle REGEXP_LIKE模糊查询用法例子
2023-07-19 08:26:22
![](https://img.aspxhome.com/file/2023/8/64448_0s.png)
Tensorflow加载模型实现图像分类识别流程详解
2023-12-22 02:31:13
![](https://img.aspxhome.com/file/2023/7/107467_0s.jpg)
简单了解python中对象的取反运算符
2021-04-21 16:50:57
![](https://img.aspxhome.com/file/2023/5/82955_0s.jpg)
Keras之自定义损失(loss)函数用法说明
2023-09-24 12:12:15
python super的使用方法及实例详解
2023-08-19 11:04:49
教你用压缩技术给SQL Server备份文件瘦身
2009-03-05 14:59:00
10个顶级Python实用库推荐
2023-08-27 17:41:46
![](https://img.aspxhome.com/file/2023/8/65518_0s.jpg)
python 实现关联规则算法Apriori的示例
2021-05-21 08:45:30
python3使用diagrams绘制架构图的步骤
2022-08-27 12:42:34
![](https://img.aspxhome.com/file/2023/8/87458_0s.png)
微信小程序实现倒计时调用相机自动拍照功能
2024-04-17 10:26:38
php去掉数组的第一个值的两个函数:array_shift、array_splice
2023-05-29 02:26:07
mysql 5.7.13 安装配置方法图文教程(win10)
2024-01-14 06:51:48
![](https://img.aspxhome.com/file/2023/6/105496_0s.jpg)
tensorflow: variable的值与variable.read_value()的值区别详解
2023-01-02 22:29:40
![](https://img.aspxhome.com/file/2023/5/85035_0s.jpg)
Mysql中的事务是什么如何使用
2024-01-21 18:42:26
Python Web版语音合成实例详解
2021-11-28 04:37:20
![](https://img.aspxhome.com/file/2023/2/85262_0s.png)
django实现用户登陆功能详解
2021-03-04 20:19:06
![](https://img.aspxhome.com/file/2023/1/88891_0s.jpg)
Ceph集群CephFS文件存储核心概念及部署使用详解
2023-04-18 02:23:31
![](https://img.aspxhome.com/file/2023/7/131897_0s.png)
pyqt5的QWebEngineView 使用模板的方法
2022-04-17 08:23:12
ASP怎么谈到应用到类的?
2008-03-10 11:21:00