-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(koa2): add the koa demos to compare koa2 and express
- Loading branch information
1 parent
6c10b65
commit ae5a732
Showing
10 changed files
with
592 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}!`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}!`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. 轻量无捆绑; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. 轻量无捆绑; | ||
|
||
|
Oops, something went wrong.