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 0ab8c56

Browse filesBrowse files
committed
add url2-koa
1 parent fa29d4f commit 0ab8c56
Copy full SHA for 0ab8c56

File tree

Expand file treeCollapse file tree

6 files changed

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

6 files changed

+149
-0
lines changed
Open diff view settings
Collapse file
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Start",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${workspaceRoot}/start.js",
9+
"stopOnEntry": false,
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"preLaunchTask": null,
13+
"runtimeExecutable": null,
14+
"runtimeArgs": [
15+
"--nolazy"
16+
],
17+
"env": {
18+
"NODE_ENV": "development"
19+
},
20+
"externalConsole": false,
21+
"sourceMaps": false,
22+
"outDir": null
23+
}
24+
]
25+
}
Collapse file
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const Koa = require('koa');
2+
3+
const bodyParser = require('koa-bodyparser');
4+
5+
const router = require('koa-router')();
6+
7+
const fs = require('fs');
8+
9+
const app = new Koa();
10+
11+
// log request URL:
12+
app.use(async (ctx, next) => {
13+
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
14+
await next();
15+
});
16+
17+
// parse request body:
18+
app.use(bodyParser());
19+
20+
// add url-route in /controllers:
21+
22+
function addMapping(router, mapping) {
23+
for (var url in mapping) {
24+
if (url.startsWith('GET ')) {
25+
var path = url.substring(4);
26+
router.get(path, mapping[url]);
27+
console.log(`register URL mapping: GET ${path}`);
28+
} else if (url.startsWith('POST ')) {
29+
var path = url.substring(5);
30+
router.post(path, mapping[url]);
31+
console.log(`register URL mapping: POST ${path}`);
32+
} else {
33+
console.log(`invalid URL: ${url}`);
34+
}
35+
}
36+
}
37+
38+
function addControllers(router) {
39+
var files = fs.readdirSync(__dirname + '/controllers');
40+
var js_files = files.filter((f) => {
41+
return f.endsWith('.js');
42+
}, files);
43+
44+
for (var f of js_files) {
45+
console.log(`process controller: ${f}...`);
46+
const mapping = require(__dirname + '/controllers/' + f);
47+
addMapping(router, mapping);
48+
}
49+
}
50+
51+
addControllers(router);
52+
53+
// add router middleware:
54+
app.use(router.routes());
55+
56+
app.listen(3000);
57+
console.log('app started at port 3000...');
Collapse file
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var fn_hello = async (ctx, next) => {
2+
var name = ctx.params.name;
3+
ctx.response.body = `<h1>Hello, ${name}!</h1>`;
4+
};
5+
6+
module.exports = {
7+
'GET /hello/:name': fn_hello
8+
};
Collapse file
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var fn_index = async (ctx, next) => {
2+
ctx.response.body = `<h1>Index</h1>
3+
<form action="/signin" method="post">
4+
<p>Name: <input name="name" value="koa"></p>
5+
<p>Password: <input name="password" type="password"></p>
6+
<p><input type="submit" value="Submit"></p>
7+
</form>`;
8+
};
9+
10+
var fn_signin = async (ctx, next) => {
11+
var
12+
name = ctx.request.body.name || '',
13+
password = ctx.request.body.password || '';
14+
console.log(`signin with name: ${name}, password: ${password}`);
15+
if (name === 'koa' && password === '12345') {
16+
ctx.response.body = `<h1>Welcome, ${name}!</h1>`;
17+
} else {
18+
ctx.response.body = `<h1>Login failed!</h1>
19+
<p><a href="/">Try again</a></p>`;
20+
}
21+
};
22+
23+
module.exports = {
24+
'GET /': fn_index,
25+
'POST /signin': fn_signin
26+
};
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "hello-koa2",
3+
"version": "1.0.0",
4+
"description": "Hello Koa 2 example with async",
5+
"main": "start.js",
6+
"scripts": {
7+
"start": "node start.js"
8+
},
9+
"keywords": [
10+
"koa",
11+
"async"
12+
],
13+
"author": "Michael Liao",
14+
"license": "Apache-2.0",
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/michaelliao/learn-javascript.git"
18+
},
19+
"dependencies": {
20+
"babel-core": "6.13.2",
21+
"babel-polyfill": "6.13.0",
22+
"babel-preset-es2015-node6": "0.3.0",
23+
"babel-preset-stage-3": "6.5.0",
24+
"koa": "2.0.0",
25+
"koa-bodyparser": "3.2.0",
26+
"koa-router": "7.0.0"
27+
}
28+
}
Collapse file
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('babel-core/register')({
2+
presets: ['stage-3']
3+
});
4+
5+
require('./app.js');

0 commit comments

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