xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Express vs Koa

Express vs Koa

https://www.esparkinfo.com/express-vs-koa.html

https://www.cleveroad.com/blog/the-best-node-js-framework-for-your-project--express-js--koa-js-or-sails-js

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://expressjs.com/

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);

https://github.com/koajs/koa

https://koajs.com/

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 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-09-03 20:28  xgqfrms  阅读(259)  评论(1编辑  收藏  举报