Skip to content

Commit 2ec21e7

Browse files
committed
add rest-hello project
1 parent 1ea8e6e commit 2ec21e7

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Lines 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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
function addMapping(router, mapping) {
22+
for (var url in mapping) {
23+
if (url.startsWith('GET ')) {
24+
var path = url.substring(4);
25+
router.get(path, mapping[url]);
26+
console.log(`register URL mapping: GET ${path}`);
27+
} else if (url.startsWith('POST ')) {
28+
var path = url.substring(5);
29+
router.post(path, mapping[url]);
30+
console.log(`register URL mapping: POST ${path}`);
31+
} else {
32+
console.log(`invalid URL: ${url}`);
33+
}
34+
}
35+
}
36+
37+
function addControllers(router) {
38+
var files = fs.readdirSync(__dirname + '/controllers');
39+
var js_files = files.filter((f) => {
40+
return f.endsWith('.js');
41+
}, files);
42+
43+
for (var f of js_files) {
44+
console.log(`process controller: ${f}...`);
45+
let mapping = require(__dirname + '/controllers/' + f);
46+
addMapping(router, mapping);
47+
}
48+
}
49+
50+
addControllers(router);
51+
52+
// add router middleware:
53+
app.use(router.routes());
54+
55+
app.listen(3000);
56+
console.log('app started at port 3000...');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var products = [{
2+
name: 'iPhone',
3+
price: 6999
4+
}, {
5+
name: 'Kindle',
6+
price: 999
7+
}];
8+
9+
module.exports = {
10+
'GET /api/products': async (ctx, next) => {
11+
ctx.response.type = 'application/json';
12+
ctx.response.body = {
13+
products: products
14+
};
15+
},
16+
17+
'POST /api/products': async (ctx, next) => {
18+
var p = {
19+
name: ctx.request.body.name,
20+
price: ctx.request.body.price
21+
};
22+
products.push(p);
23+
ctx.response.type = 'application/json';
24+
ctx.response.body = p;
25+
}
26+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "rest-koa",
3+
"version": "1.0.0",
4+
"description": "koa 2 example with rest api",
5+
"main": "start.js",
6+
"scripts": {
7+
"start": "node start.js"
8+
},
9+
"keywords": [
10+
"koa",
11+
"rest",
12+
"api"
13+
],
14+
"author": "Michael Liao",
15+
"license": "Apache-2.0",
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/michaelliao/learn-javascript.git"
19+
},
20+
"dependencies": {
21+
"babel-core": "6.13.2",
22+
"babel-polyfill": "6.13.0",
23+
"babel-preset-es2015-node6": "0.3.0",
24+
"babel-preset-stage-3": "6.5.0",
25+
"koa": "2.0.0",
26+
"koa-bodyparser": "3.2.0",
27+
"koa-router": "7.0.0"
28+
}
29+
}
Lines 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)