express-cargo
面向 Express.js 的声明式、基于装饰器的请求数据处理。
import express from 'express'
import { Body, bindingCargo, getCargo, Min, Header, Params } from 'express-cargo'
const app = express()
app.use(express.json())
class RequestExample {
@Body()
name!: string
@Body()
@Min(0)
age!: number
@Params('id')
id!: number
@Header()
authorization!: string
}
app.post('/:id', bindingCargo(RequestExample), (req, res) => {
const data = getCargo<RequestExample>(req)
// write your code with bound data
})
app.listen(3000)
声明式数据绑定
使用直观的装饰器,将请求数据(body、query、params)轻松绑定到类实例,让路由逻辑保持清晰聚焦。
可靠的数据验证
借助丰富的内置装饰器轻松验证数据,并在处理前确保数据完整性。
类型安全且可扩展
基于 TypeScript 构建,提供完整的类型安全,并允许你通过自定义装饰器和转换器扩展库。