分享DWS 基于 Deno 平台的 web 开发框架
发布于 8 个月前2695
DWS
https://github.com/xingqiao/dws
类似 Koa,按照类似堆栈的方式组织和执行
- 自带路由实现,支持字符串完全匹配或正则匹配
- 静态资源托管
- 支持 Nunjucks 模板引擎
// index.ts
import { DWS, useStatic } from 'https://gitee.com/ccts/dws/raw/master/index.ts';
const app = new DWS();
// 设置中间件
app.use(async (ctx, next) => {
ctx.body = 'Hello DWS';
await next();
});
// 字符串方式匹配
app.use('/hallo', async ctx => {
ctx.body = 'Hello DWS';
});
// 正则方式匹配
app.use(/\w+\/hallo/i, ctx => {
ctx.redirect('/hallo');
});
// 使用模板引擎
app.use('GET', '/index.html', async ctx => {
ctx.body = await ctx.renderString('index.html', { username: 'James' });
});
// 设置静态资源路由
app.use(useStatic('public', {
maxAge: 600
}));
// 监听端口
app.listen(8000);
启动
deno run --allow-net --allow-read index.ts
共有 2 条回复
补上和 Koa 的性能对比
- Running 5s
- 100 connections with 2 pipelining factor
DWS
Stat 2.5% 50% 97.5% 99% Avg Stdev Max Latency 1 ms 2 ms 3 ms 4 ms 1.89 ms 1.34 ms 111.15 ms Stat 1% 2.5% 50% 97.5% Avg Stdev Min Req/Sec 35647 35647 42719 42975 41308.81 2844.13 35647 Bytes/Sec 4.46 MB 4.46 MB 5.34 MB 5.37 MB 5.16 MB 355 kB 4.46 MB 207k requests in 5.06s, 25.8 MB read
Koa
Stat 2.5% 50% 97.5% 99% Avg Stdev Max Latency 0 ms 1 ms 5 ms 8 ms 2.17 ms 2.9 ms 84.46 ms Stat 1% 2.5% 50% 97.5% Avg Stdev Min Req/Sec 29599 29599 43103 43967 40606.4 5529.94 29595 Bytes/Sec 4.5 MB 4.5 MB 6.55 MB 6.68 MB 6.17 MB 840 kB 4.5 MB 203k requests in 5.05s, 30.9 MB read
error: TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'. Type 'URL' is not assignable to type 'string'. return new URL(url).pathname
~at https://deno.land/std@0.57.0/path/win32.ts:917:18TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'. Type 'URL' is not assignable to type 'string'. return new URL(url).pathname;
~at https://deno.land/std@0.57.0/path/posix.ts:438:18Found 2 errors.
尝试了一下. 报了这个错误. 是更新了标准库的原因吗.
deno --version deno 1.2.2 v8 8.5.216 typescript 3.9.2
登录后发表评论!
通过Github登录