
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
@karinjs/express
Advanced tools
这是一个完全使用 TypeScript 重写的 Express 5.x 框架,旨在提供更好的类型安全性和开发体验,同时保持与原版 Express 的 API 兼容性。
本项目专为 github.com/karinjs/karin 优化而开发,针对其特定使用场景进行了深度定制和性能优化。
[!WARNING] 本项目未经生产环境充分验证,不建议非专业用户使用。
- 🔴 本项目仍在积极开发中
- 🔴 未经过大规模生产环境的长期验证
- 🔴 建议仅在开发和测试环境中使用
- 🔴 生产环境使用需自行承担风险
本项目采用 pnpm workspace 管理,包含以下子包:
packages:
- "packages/path-to-regexp" # 路径匹配和参数提取
- "packages/send" # 文件发送类型定义
- "packages/connect" # Connect 中间件类型定义
- "packages/serve-static" # 静态文件服务
src/
├── application.ts # Application 类 - 核心应用逻辑
├── express.ts # Express 工厂函数和导出
├── request.ts # Request 类 - 扩展的请求对象
├── response.ts # Response 类 - 扩展的响应对象
├── view.ts # 视图渲染系统
├── utils.ts # 工具函数
├── router/ # 路由系统
│ ├── index.ts # Router 类
│ ├── layer.ts # 路由层
│ └── route.ts # Route 类
├── body-parser/ # 请求体解析器
│ └── src/
└── serve-static/ # 静态文件服务实现
└── index.ts
@types/express 提供类型// 更好的类型推导
const app = express()
app.get('/user/:id', (req, res) => {
// req.params 自动推导类型
const id = req.params.id // string
res.json({ id })
})
path-to-regexp、send 等)tsdown 构建,生成 ESM 格式// tsdown.config.ts
export default defineConfig({
entry: ['./src/index.ts'],
format: ['esm'],
dts: { resolve: true, build: true },
target: 'node18',
platform: 'node',
})
// 泛型支持示例
class Request<
P = ParamsDictionary, // 路径参数类型
ResBody = any, // 响应体类型
ReqBody = any, // 请求体类型
ReqQuery = ParsedQs, // 查询参数类型
LocalsObj = Record<string, any> // 本地变量类型
> extends IncomingMessage
// Application.status() - 更严格的状态码验证
status(code: StatusCode): this {
if (!Number.isInteger(code)) {
throw new TypeError(`Invalid status code: ${JSON.stringify(code)}`)
}
if (code < 100 || code > 999) {
throw new RangeError(`Invalid status code: ${JSON.stringify(code)}`)
}
this.statusCode = code
return this
}
{
"paths": {
"router": ["./src/router/index.ts"],
"body-parser": ["./src/body-parser/src/index.ts"],
"serve-static": ["./src/serve-static/index.ts"]
}
}
npm install @karinjs/express
# 或
pnpm add @karinjs/express
# 或
yarn add @karinjs/express
import express from '@karinjs/express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000')
})
import { RequestHandler } from '@karinjs/express'
const authMiddleware: RequestHandler = (req, res, next) => {
// 完整的类型提示
const token = req.get('Authorization')
if (!token) {
return res.status(401).json({ error: 'Unauthorized' })
}
next()
}
interface UserParams {
id: string
}
interface UserBody {
name: string
email: string
}
app.post<UserParams, any, UserBody>('/user/:id', (req, res) => {
const { id } = req.params // 类型: { id: string }
const { name, email } = req.body // 类型: { name: string, email: string }
res.json({ id, name, email })
})
// 无需额外安装 body-parser
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.post('/api/data', (req, res) => {
console.log(req.body) // 自动解析
res.json({ received: true })
})
// 使用内置的 serve-static
app.use(express.static('public'))
app.use('/static', express.static('assets'))
本项目保持与 Express 5.x 的 API 兼容性。主要的 API 包括:
app.get(path, ...handlers)app.post(path, ...handlers)app.put(path, ...handlers)app.delete(path, ...handlers)app.use([path], ...middleware)app.listen(port, [callback])app.set(setting, value)app.engine(ext, callback)app.render(view, [locals], callback)req.params - 路径参数req.query - 查询参数req.body - 请求体req.get(field) - 获取请求头req.accepts(types) - 内容协商req.is(type) - 检查 Content-Typeres.status(code) - 设置状态码res.send(body) - 发送响应res.json(obj) - 发送 JSONres.redirect([status], url) - 重定向res.render(view, [locals], [callback]) - 渲染视图res.sendFile(path, [options], [callback]) - 发送文件# 运行所有测试
pnpm test
# 运行特定模块测试
pnpm test:router
pnpm test:body-parser
pnpm test:serve-static
# 运行验收测试
pnpm test:acceptance
# 生成覆盖率报告
pnpm test-cov
# 克隆仓库
git clone https://github.com/sj817/express.git
cd express
# 安装依赖
pnpm install
# 构建项目
pnpm build
# 运行 Lint
pnpm lint
# 修复 Lint 问题
pnpm lint:fix
原版 Express 的类型定义存在于 @types/express,与实现分离,容易出现类型不匹配的问题。TypeScript 原生实现可以确保类型与代码完全一致。
Monorepo 架构使得核心依赖都在一个仓库中,便于整体维护和优化,减少版本不兼容的风险。
完全掌握代码实现,方便根据项目需求进行定制和优化。
欢迎提交 Issue 和 Pull Request!
本项目使用 tsdown 进行打包,最终产物为零依赖的纯 ESM 模块:
✨ 打包产物信息:
ℹ dist/index.mjs 904.62 kB │ gzip: 278.74 kB
ℹ dist/index.d.ts 75.65 kB │ gzip: 20.25 kB
ℹ 2 files, total: 980.27 kB
📊 对比数据:
源包大小: 2.2MB / 65 packages (数据来源: https://pkg-size.dev/express)
打包后: 980.27 kB / 0 dependencies ⚡
打包后的代码完全独立运行,无需安装任何额外依赖:
由于采用 ESM 格式,代码可完全参与任何现代化打包器的优化:
妈妈再也不用担心打包器无法处理 Express 啦! 🎉
本项目基于 Express 进行重写,感谢 Express 团队及所有贡献者的杰出工作。
本项目将以下核心依赖整合到 Monorepo 中,并进行了 TypeScript 重写:
以下依赖库在打包时被内联,最终产物为零依赖:
如果这个项目对你有帮助,请给一个 ⭐️ Star 支持一下!
FAQs
Fast, unopinionated, minimalist web framework
The npm package @karinjs/express receives a total of 85 weekly downloads. As such, @karinjs/express popularity was classified as not popular.
We found that @karinjs/express demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.