Skip to content

Commit 50bcadf

Browse files
isdongyuhaoxin
authored and
haoxin
committed
migrate to koa@2 (koajs#108)
* support async/await * change nodejs version 4 or 6 to 5.6 * eslint * remove jade
1 parent 7fc965b commit 50bcadf

File tree

36 files changed

+349
-354
lines changed

36 files changed

+349
-354
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_js:
2-
- "4"
3-
- "6"
2+
- stable
3+
- 7.6
44
language: node_js
55
script:
66
- make lint

404/app.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
var koa = require('koa');
1+
const Koa = require('koa');
22

3-
var app = module.exports = koa();
4-
5-
app.use(function *pageNotFound(next) {
6-
yield next;
7-
8-
if (404 != this.status) return;
3+
const app = module.exports = new Koa();
94

5+
app.use(async function pageNotFound(ctx) {
106
// we need to explicitly set 404 here
117
// so that koa doesn't assign 200 on body=
12-
this.status = 404;
8+
ctx.status = 404;
139

14-
switch (this.accepts('html', 'json')) {
10+
switch (ctx.accepts('html', 'json')) {
1511
case 'html':
16-
this.type = 'html';
17-
this.body = '<p>Page Not Found</p>';
12+
ctx.type = 'html';
13+
ctx.body = '<p>Page Not Found</p>';
1814
break;
1915
case 'json':
20-
this.body = {
16+
ctx.body = {
2117
message: 'Page Not Found'
2218
};
2319
break;
2420
default:
25-
this.type = 'text';
26-
this.body = 'Page Not Found';
21+
ctx.type = 'text';
22+
ctx.body = 'Page Not Found';
2723
}
2824
});
2925

404/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
var app = require('./app');
3-
var request = require('supertest').agent(app.listen());
2+
const app = require('./app');
3+
const request = require('supertest').agent(app.listen());
44

55
describe('404', function() {
66
describe('when GET /', function() {

base-auth/app.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
var koa = require('koa');
2-
var auth = require('koa-basic-auth');
3-
var app = module.exports = koa();
1+
const Koa = require('koa');
2+
const auth = require('koa-basic-auth');
3+
4+
const app = module.exports = new Koa();
45

56
// custom 401 handling
67

7-
app.use(function* (next) {
8+
app.use(async function(ctx, next) {
89
try {
9-
yield next;
10+
await next();
1011
} catch (err) {
11-
if (401 == err.status) {
12-
this.status = 401;
13-
this.set('WWW-Authenticate', 'Basic');
14-
this.body = 'cant haz that';
12+
if (err.status === 401) {
13+
ctx.status = 401;
14+
ctx.set('WWW-Authenticate', 'Basic');
15+
ctx.body = 'cant haz that';
1516
} else {
1617
throw err;
1718
}
@@ -24,8 +25,8 @@ app.use(auth({ name: 'tj', pass: 'tobi' }));
2425

2526
// secret response
2627

27-
app.use(function* () {
28-
this.body = 'secret';
28+
app.use(async function(ctx) {
29+
ctx.body = 'secret';
2930
});
3031

3132
if (!module.parent) app.listen(3000);

blog/app.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
11

2-
/**
3-
* Module dependencies.
4-
*/
2+
const render = require('./lib/render');
3+
const logger = require('koa-logger');
4+
const router = require('koa-router')();
5+
const koaBody = require('koa-body');
56

6-
var render = require('./lib/render');
7-
var logger = require('koa-logger');
8-
var route = require('koa-route');
9-
var parse = require('co-body');
10-
var koa = require('koa');
11-
var app = module.exports = koa();
7+
const Koa = require('koa');
8+
const app = module.exports = new Koa();
129

1310
// "database"
1411

15-
var posts = [];
12+
const posts = [];
1613

1714
// middleware
1815

1916
app.use(logger());
2017

21-
// route middleware
18+
app.use(render);
2219

23-
app.use(route.get('/', list));
24-
app.use(route.get('/post/new', add));
25-
app.use(route.get('/post/:id', show));
26-
app.use(route.post('/post', create));
20+
app.use(koaBody());
2721

2822
// route definitions
2923

24+
router.get('/', list)
25+
.get('/post/new', add)
26+
.get('/post/:id', show)
27+
.post('/post', create);
28+
29+
app.use(router.routes());
30+
3031
/**
3132
* Post listing.
3233
*/
3334

34-
function *list() {
35-
this.body = yield render('list', { posts: posts });
35+
async function list(ctx) {
36+
await ctx.render('list', { posts: posts });
3637
}
3738

3839
/**
3940
* Show creation form.
4041
*/
4142

42-
function *add() {
43-
this.body = yield render('new');
43+
async function add(ctx) {
44+
await ctx.render('new');
4445
}
4546

4647
/**
4748
* Show post :id.
4849
*/
4950

50-
function *show(id) {
51-
var post = posts[id];
52-
if (!post) this.throw(404, 'invalid post id');
53-
this.body = yield render('show', { post: post });
51+
async function show(ctx) {
52+
const id = ctx.params.id;
53+
const post = posts[id];
54+
if (!post) ctx.throw(404, 'invalid post id');
55+
await ctx.render('show', { post: post });
5456
}
5557

5658
/**
5759
* Create a post.
5860
*/
5961

60-
function *create() {
61-
var post = yield parse(this);
62-
var id = posts.push(post) - 1;
62+
async function create(ctx) {
63+
const post = ctx.request.body;
64+
const id = posts.push(post) - 1;
6365
post.created_at = new Date();
6466
post.id = id;
65-
this.redirect('/');
67+
ctx.redirect('/');
6668
}
6769

6870
// listen
6971

7072
if (!module.parent) app.listen(3000);
71-

blog/lib/render.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* Module dependencies.
44
*/
55

6-
var views = require('co-views');
7-
var path = require('path');
6+
const views = require('koa-views');
7+
const path = require('path');
88

99
// setup views mapping .html
1010
// to the swig template engine

blog/test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var app = require('./app');
2-
var request = require('supertest').agent(app.listen());
1+
const app = require('./app');
2+
const request = require('supertest').agent(app.listen());
3+
require('should');
34

45
describe('Blog', function() {
56
describe('GET /', function() {
@@ -8,12 +9,12 @@ describe('Blog', function() {
89
.get('/')
910
.expect(200, function(err, res) {
1011
if (err) return done(err);
11-
1212
res.should.be.html;
1313
res.text.should.include('<title>Posts</title>');
1414
done();
1515
});
1616
});
17+
1718
it('should see 0 post', function(done) {
1819
request
1920
.get('/')
@@ -26,6 +27,7 @@ describe('Blog', function() {
2627
});
2728
});
2829
});
30+
2931
describe('POST /post/new', function() {
3032
it('should create post and redirect to /', function(done) {
3133
request
@@ -39,6 +41,7 @@ describe('Blog', function() {
3941
});
4042
});
4143
});
44+
4245
describe('GET /post/0', function() {
4346
it('should see post', function(done) {
4447
request

body-parsing/app.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11

2-
var koa = require('koa');
3-
var parse = require('co-body');
2+
const Koa = require('koa');
3+
const koaBody = require('koa-body');
44

5-
var app = module.exports = koa();
5+
const app = module.exports = new Koa();
6+
7+
app.use(koaBody({
8+
jsonLimit: '1kb'
9+
}));
610

711
// POST .name to /uppercase
812
// co-body accepts application/json
913
// and application/x-www-form-urlencoded
1014

11-
app.use(function *(next) {
12-
if ('POST' != this.method) return yield next;
13-
var body = yield parse(this, { limit: '1kb' });
14-
if (!body.name) this.throw(400, '.name required');
15-
this.body = { name: body.name.toUpperCase() };
15+
app.use(async function(ctx) {
16+
const body = ctx.request.body;
17+
if (!body.name) ctx.throw(400, '.name required');
18+
ctx.body = { name: body.name.toUpperCase() };
1619
});
1720

1821
if (!module.parent) app.listen(3000);

compose/app.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@
1212
* ]))
1313
*/
1414

15-
var compose = require('koa-compose');
16-
var koa = require('koa');
17-
var app = module.exports = koa();
15+
const compose = require('koa-compose');
16+
const Koa = require('koa');
17+
const app = module.exports = new Koa();
1818

1919
// x-response-time
2020

21-
function *responseTime(next) {
22-
var start = new Date();
23-
yield next;
24-
var ms = new Date() - start;
25-
this.set('X-Response-Time', ms + 'ms');
21+
async function responseTime(ctx, next) {
22+
const start = new Date();
23+
await next();
24+
const ms = new Date() - start;
25+
ctx.set('X-Response-Time', ms + 'ms');
2626
}
2727

2828
// logger
2929

30-
function* logger(next) {
31-
var start = new Date();
32-
yield next;
33-
var ms = new Date() - start;
30+
async function logger(ctx, next) {
31+
const start = new Date();
32+
await next();
33+
const ms = new Date() - start;
3434
if ('test' != process.env.NODE_ENV) {
35-
console.log('%s %s - %s', this.method, this.url, ms);
35+
console.log('%s %s - %s', this.method, ctx.url, ms);
3636
}
3737
}
3838

3939
// response
4040

41-
function* respond(next) {
42-
yield next;
43-
if ('/' != this.url) return;
44-
this.body = 'Hello World';
41+
async function respond(ctx, next) {
42+
await next();
43+
if ('/' != ctx.url) return;
44+
ctx.body = 'Hello World';
4545
}
4646

4747
// composed middleware
4848

49-
var all = compose([
49+
const all = compose([
5050
responseTime,
5151
logger,
5252
respond

compose/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var app = require('./app');
2-
var request = require('supertest').agent(app.listen());
1+
const app = require('./app');
2+
const request = require('supertest').agent(app.listen());
33

44
describe('Compose', function() {
55
describe('when GET /', function() {

conditional-middleware/app.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
var logger = require('koa-logger');
2-
var koa = require('koa');
3-
var app = koa();
1+
const logger = require('koa-logger');
2+
const Koa = require('koa');
3+
const app = new Koa();
44

55
// passing any middleware to this middleware
66
// will make it conditional, and will not be used
77
// when an asset is requested, illustrating how
88
// middleware may "wrap" other middleware.
99

1010
function ignoreAssets(mw) {
11-
return function *(next) {
12-
if (/(\.js|\.css|\.ico)$/.test(this.path)) {
13-
yield next;
11+
return async function(ctx, next) {
12+
if (/(\.js|\.css|\.ico)$/.test(ctx.path)) {
13+
await next();
1414
} else {
1515
// must .call() to explicitly set the receiver
16-
// so that "this" remains the koa Context
17-
yield mw.call(this, next);
16+
await mw.call(this, ctx, next);
1817
}
1918
};
2019
}
@@ -26,8 +25,8 @@ function ignoreAssets(mw) {
2625

2726
app.use(ignoreAssets(logger()));
2827

29-
app.use(function *() {
30-
this.body = 'Hello World';
28+
app.use(async function(ctx) {
29+
ctx.body = 'Hello World';
3130
});
3231

3332
app.listen(3000);

0 commit comments

Comments
 (0)