bandeng-next
v1.0.2
Published
From bandeng developer team.
Readme
Bandeng Next
English
A next function toolkit for Node.js microservices, supporting Express and Koa framework styles.
Features
- Lightweight implementation
- Async middleware support
- Framework compatibility
- Response time calculation
- Middleware chain execution
Installation
npm install bandeng-nextUsage
Koa Style
const NextGenerator = require('bandeng-next')
const next = new NextGenerator('koa').getNextFunc()
async function calcTime(ctx, next) {
const start = Date.now()
await next()
const end = Date.now()
console.log(`Response Time: ${end - start}ms`)
}
async function responseMiddleware(ctx, next) {
ctx.body = { code: 2000, data: 'success' }
next()
}
next({}, [calcTime, responseMiddleware])Express Style
const NextGenerator = require('bandeng-next')
const next = new NextGenerator('express').getNextFunc()
async function calcTime(req, res, next) {
const start = Date.now()
await next()
const end = Date.now()
console.log(`Response Time: ${end - start}ms`)
}
async function responseMiddleware(req, res, next) {
res.json({ code: 2000, data: 'success' })
next()
}
next({}, {}, [calcTime, responseMiddleware])中文
一个面向 Node.js 微服务的 next 工具包,支持 Express 和 Koa 框架风格。
特性
- 轻量级实现
- 支持异步中间件
- 框架兼容性
- 响应时间计算
- 中间件链执行
安装
npm install bandeng-next使用方法
Koa 风格
const NextGenerator = require('bandeng-next')
const next = new NextGenerator('koa').getNextFunc()
async function calcTime(ctx, next) {
const start = Date.now()
await next()
const end = Date.now()
console.log(`响应时间: ${end - start}ms`)
}
async function responseMiddleware(ctx, next) {
ctx.body = { code: 2000, data: 'success' }
next()
}
next({}, [calcTime, responseMiddleware])Express 风格
const NextGenerator = require('bandeng-next')
const next = new NextGenerator('express').getNextFunc()
async function calcTime(req, res, next) {
const start = Date.now()
await next()
const end = Date.now()
console.log(`响应时间: ${end - start}ms`)
}
async function responseMiddleware(req, res, next) {
res.json({ code: 2000, data: 'success' })
next()
}
next({}, {}, [calcTime, responseMiddleware])作者
- lizhi
许可证
ISC
