Express vs Koa
Express vs Koa
https://www.esparkinfo.com/express-vs-koa.html
Express
$ yarn add express
# OR
$ npm i express
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
https://github.com/expressjs/express
5.x
https://expressjs.com/en/5x/api.html
https://github.com/expressjs/express/blob/5.0/History.md
Koa
$ yarn add koa
# OR
$ npm i koa
const Koa = require('koa');
const app = new Koa();
// response
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Koa middleware
const Koa = require('koa');
const app = new Koa();
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});
// x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
// response
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
refs
https://github.com/koajs/koa/blob/master/docs/koa-vs-express.md
https://medium.com/@theomalaper.cognez/express-vs-koa-and-hapi-a2c65f949b78
https://nodesource.com/blog/Express-Koa-Hapi
https://www.sitepoint.com/express-koa-hapi/
https://raygun.com/blog/koa-vs-express/
https://www.cmsdrupal.com/blog/koa-vs-express-how-koa-middleware-different-express-middleware
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13605233.html
未经授权禁止转载,违者必究!