Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
45
Fork
21
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
koa和express有哪些不同?
Backlog
#IAG9LE
陌生人
owner
Opened this issue
2024-07-29 16:04
<h4>框架介绍</h4><p>express框架是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架,主要基于 Connect 中间件,并且自身封装了路由、视图处理等功能。</p><p>koa是 Express 原班人马基于 ES6 新特性重新开发的框架,主要基于 co 中间件,框架自身不包含任何中间件,很多功能需要借助第三方中间件解决,但是由于其基于 ES6 generator 特性的异步流程控制,解决了 "callback hell" 和麻烦的错误处理问题。</p><h4>相同点</h4><p>两个框架都对http进行了封装。相关的api都差不多,同一批人所写。</p><h4>不同点</h4><p>express内置了许多中间件可供使用,而koa没有。</p><p>express包含路由,视图渲染等特性,而koa只有http模块。</p><p>express的中间件模型为线型,而koa的中间件模型为U型,也可称为洋葱模型构造中间件。</p><p>express通过回调实现异步函数,在多个回调、多个中间件中写起来容易逻辑混乱。</p><pre><code class="language-js">// express写法 app.get('/test', function (req, res) { fs.readFile('/file1', function (err, data) { if (err) { res.status(500).send('read file1 error'); } fs.readFile('/file2', function (err, data) { if (err) { res.status(500).send('read file2 error'); } res.type('text/plain'); res.send(data); }); }); }); </code></pre><p>koa通过generator 和 async/await 使用同步的写法来处理异步,明显好于 callback 和 promise。</p><pre><code class="language-js">app.use(async (ctx, next) => { await next(); var data = await doReadFile(); ctx.response.type = 'text/plain'; ctx.response.body = data; }); </code></pre><h4><strong>总结</strong></h4><p><strong>Express</strong><br><br>优点:线性逻辑,通过中间件形式把业务逻辑细分、简化,一个请求进来经过一系列中间件处理后再响应给用户,清晰明了。<br><br>缺点:基于 callback 组合业务逻辑,业务逻辑复杂时嵌套过多,异常捕获困难。</p><p><strong>Koa</strong><br><br>优点:首先,借助 co 和 generator,很好地解决了异步流程控制和异常捕获问题。其次,Koa 把 Express 中内置的 router、view 等功能都移除了,使得框架本身更轻量。<br><br>缺点:社区相对较小。</p>
<h4>框架介绍</h4><p>express框架是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架,主要基于 Connect 中间件,并且自身封装了路由、视图处理等功能。</p><p>koa是 Express 原班人马基于 ES6 新特性重新开发的框架,主要基于 co 中间件,框架自身不包含任何中间件,很多功能需要借助第三方中间件解决,但是由于其基于 ES6 generator 特性的异步流程控制,解决了 "callback hell" 和麻烦的错误处理问题。</p><h4>相同点</h4><p>两个框架都对http进行了封装。相关的api都差不多,同一批人所写。</p><h4>不同点</h4><p>express内置了许多中间件可供使用,而koa没有。</p><p>express包含路由,视图渲染等特性,而koa只有http模块。</p><p>express的中间件模型为线型,而koa的中间件模型为U型,也可称为洋葱模型构造中间件。</p><p>express通过回调实现异步函数,在多个回调、多个中间件中写起来容易逻辑混乱。</p><pre><code class="language-js">// express写法 app.get('/test', function (req, res) { fs.readFile('/file1', function (err, data) { if (err) { res.status(500).send('read file1 error'); } fs.readFile('/file2', function (err, data) { if (err) { res.status(500).send('read file2 error'); } res.type('text/plain'); res.send(data); }); }); }); </code></pre><p>koa通过generator 和 async/await 使用同步的写法来处理异步,明显好于 callback 和 promise。</p><pre><code class="language-js">app.use(async (ctx, next) => { await next(); var data = await doReadFile(); ctx.response.type = 'text/plain'; ctx.response.body = data; }); </code></pre><h4><strong>总结</strong></h4><p><strong>Express</strong><br><br>优点:线性逻辑,通过中间件形式把业务逻辑细分、简化,一个请求进来经过一系列中间件处理后再响应给用户,清晰明了。<br><br>缺点:基于 callback 组合业务逻辑,业务逻辑复杂时嵌套过多,异常捕获困难。</p><p><strong>Koa</strong><br><br>优点:首先,借助 co 和 generator,很好地解决了异步流程控制和异常捕获问题。其次,Koa 把 Express 中内置的 router、view 等功能都移除了,使得框架本身更轻量。<br><br>缺点:社区相对较小。</p>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
Node
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
No related branch
master
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register