Skip to content

Commit

Permalink
feat(koa2): add the koa demos to compare koa2 and express
Browse files Browse the repository at this point in the history
  • Loading branch information
huangwenming committed Dec 16, 2019
1 parent 6c10b65 commit ae5a732
Show file tree
Hide file tree
Showing 10 changed files with 592 additions and 0 deletions.
4 changes: 4 additions & 0 deletions html-relevant/modules/fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const repeat = (string) => `${string} ${string}`;
export function shout(string) {
return `${string.toUpperCase()}!`;
}
10 changes: 10 additions & 0 deletions html-relevant/modules/jsModule.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
$END$
</body>
</html>
Empty file added html-relevant/modules/lib.js
Empty file.
4 changes: 4 additions & 0 deletions html-relevant/modules/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const repeat = (string) => `${string} ${string}`;
export function shout(string) {
return `${string.toUpperCase()}!`;
}
11 changes: 11 additions & 0 deletions node-study/koa2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Koa2 知识点
> 新一代web服务框架,用于取代express
## Features
1. 使用Promise配合async来实现异步;
2. 采用中间件架构;
3. 优雅的API封装;
4. 增强错误处理;
5. 轻量无捆绑;


57 changes: 57 additions & 0 deletions node-study/koa2/examples/basic-use/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @file koa2 基础使用
* @description 主要用于和express进行对比
* @author hwm
*/
const Koa = require('koa');
const KoaStatic = require('koa-static');
const KoaRouter = require('koa-router');

const app = new Koa();

// 静态服务
app.use(KoaStatic('./statics'));

// 中间件
app.use((context, next)=> {
context.body = {
msg: 'hello, i am koa2'
};
next();
});

// 日志
app.use(async(context, next)=> {
const start = new Date().getTime();
await next();
const end = new Date().getTime();
console.log(`the time consuming of the gethtml api :${end - start}ms`);
});

app.use((context, next)=> {
if (context.url === '/gethtml') {
context.type = 'text/html; charset=utf-8';
context.body = `<h3>${context.body.msg}</h3>`;
}
next();
});


// 路由
const router = new KoaRouter();
router.get('/html', (context, next)=> {
context.type = 'text/html; charset=utf-8';
context.body = `<h2>${context.body.msg}</h2>`;
next();
});
app.use(router.routes());


app.listen(3000, err => {
if (!err) {
console.log('server is listenning on port 3000');
}
else {
console.log('server fails to start on port 3000');
}
});
43 changes: 43 additions & 0 deletions node-study/koa2/examples/basic-use/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file express 基础使用
* @description 主要用于和koa进行对比
* @author hwm
*/
const express = require('express');
const router = express.Router();

const app = new express();

// 静态服务
app.use(express.static('./statics'));

// 路由
app.get('/gethtml', (req, res)=> {
res.type('html');
res.send(`<h2>hello, i am express</h2>`);
});

// invoked for any requests passed to this router
router.use(function (req, res, next) {
// .. some logic here .. like any other middleware
next()
});

// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function (req, res, next) {
// ..
});

// only requests to /calendar/* will be sent to our "router"
app.use('/calendar', router);


app.listen(3001, err => {
if (!err) {
console.log('server is listenning on port 3001');
}
else {
console.log('server fails to start on port 3001');
}
});
11 changes: 11 additions & 0 deletions node-study/koa2/examples/basic-use/statics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Koa2 知识点
> 新一代web服务框架,用于取代express
## Features
1. 使用Promise配合async来实现异步;
2. 采用中间件架构;
3. 优雅的API封装;
4. 增强错误处理;
5. 轻量无捆绑;


Loading

0 comments on commit ae5a732

Please sign in to comment.