Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b4f7a60

Browse filesBrowse files
committed
add koa app test sample
1 parent bd72859 commit b4f7a60
Copy full SHA for b4f7a60

File tree

Expand file treeCollapse file tree

4 files changed

+77
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+77
-0
lines changed
Open diff view settings
Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Koa = require('koa');
2+
3+
const app = new Koa();
4+
5+
app.use(async (ctx, next) => {
6+
const start = new Date().getTime();
7+
await next();
8+
const ms = new Date().getTime() - start;
9+
console.log(`${ctx.request.method} ${ctx.request.url}: ${ms}ms`);
10+
ctx.response.set('X-Response-Time', `${ms}ms`);
11+
});
12+
13+
app.use(async (ctx, next) => {
14+
var name = ctx.request.query.name || 'world';
15+
ctx.response.type = 'text/html';
16+
ctx.response.body = `<h1>Hello, ${name}!</h1>`;
17+
});
18+
19+
module.exports = app;
Collapse file
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "koa-test",
3+
"version": "1.0.0",
4+
"description": "Test Koa 2 app",
5+
"main": "start.js",
6+
"scripts": {
7+
"start": "node --use_strict start.js",
8+
"test": "mocha"
9+
},
10+
"keywords": [
11+
"koa",
12+
"async",
13+
"mocha",
14+
"test"
15+
],
16+
"author": "Michael Liao",
17+
"license": "Apache-2.0",
18+
"repository": {
19+
"type": "git",
20+
"url": "https://github.com/michaelliao/learn-javascript.git"
21+
},
22+
"dependencies": {
23+
"koa": "2.0.0"
24+
},
25+
"devDependencies": {
26+
"mocha": "3.0.2",
27+
"supertest": "3.0.0"
28+
}
29+
}
Collapse file
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const app = require('./app');
2+
3+
app.listen(3000);
4+
console.log('app started at port 3000...');
Collapse file
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const
2+
request = require('supertest'),
3+
app = require('../app');
4+
5+
describe('#test koa app', () => {
6+
7+
let server = app.listen(9900);
8+
9+
describe('#test server', () => {
10+
11+
it('#test GET /', async () => {
12+
let res = await request(server)
13+
.get('/')
14+
.expect('Content-Type', /text\/html/)
15+
.expect(200, '<h1>Hello, world!</h1>');
16+
});
17+
18+
it('#test GET /path?name=Bob', async () => {
19+
let res = await request(server)
20+
.get('/path?name=Bob')
21+
.expect('Content-Type', /text\/html/)
22+
.expect(200, '<h1>Hello, Bob!</h1>');
23+
});
24+
});
25+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.