Skip to content

Commit 079bd55

Browse files
committed
add project view-loa
1 parent 411daa2 commit 079bd55

28 files changed

+10459
-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+
}

samples/node/web/koa/view-koa/app.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const Koa = require('koa');
2+
3+
const bodyParser = require('koa-bodyparser');
4+
5+
const router = require('koa-router')();
6+
7+
const templating = require('./templating.js');
8+
9+
const fs = require('fs');
10+
11+
const app = new Koa();
12+
13+
const isProduction = process.env.NODE_ENV === 'production';
14+
15+
// log request URL:
16+
app.use(async (ctx, next) => {
17+
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
18+
var
19+
start = new Date().getTime(),
20+
execTime;
21+
await next();
22+
execTime = new Date().getTime() - start;
23+
ctx.response.set('X-Response-Time', `${execTime}ms`);
24+
});
25+
26+
// static file support:
27+
if (! isProduction) {
28+
let staticFiles = require('./static-files');
29+
app.use(staticFiles('/static/', __dirname + '/static'));
30+
}
31+
32+
// parse request body:
33+
app.use(bodyParser());
34+
35+
// add nunjucks as view:
36+
app.use(templating('view', {
37+
noCache: !isProduction,
38+
watch: !isProduction
39+
}));
40+
41+
// add url-route in /controllers:
42+
43+
function addMapping(router, mapping) {
44+
for (var url in mapping) {
45+
if (url.startsWith('GET ')) {
46+
var path = url.substring(4);
47+
router.get(path, mapping[url]);
48+
console.log(`register URL mapping: GET ${path}`);
49+
} else if (url.startsWith('POST ')) {
50+
var path = url.substring(5);
51+
router.post(path, mapping[url]);
52+
console.log(`register URL mapping: POST ${path}`);
53+
} else {
54+
console.log(`invalid URL: ${url}`);
55+
}
56+
}
57+
}
58+
59+
function addControllers(router) {
60+
var files = fs.readdirSync(__dirname + '/controllers');
61+
var js_files = files.filter((f) => {
62+
return f.endsWith('.js');
63+
}, files);
64+
65+
for (var f of js_files) {
66+
console.log(`process controller: ${f}...`);
67+
let mapping = require(__dirname + '/controllers/' + f);
68+
addMapping(router, mapping);
69+
}
70+
}
71+
72+
addControllers(router);
73+
74+
// add router middleware:
75+
app.use(router.routes());
76+
77+
app.listen(3000);
78+
console.log('app started at port 3000...');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// index:
2+
3+
module.exports = {
4+
'GET /': async (ctx, next) => {
5+
ctx.render('index.html', {
6+
title: 'Welcome'
7+
});
8+
}
9+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// sign in:
2+
3+
module.exports = {
4+
'POST /signin': async (ctx, next) => {
5+
var
6+
email = ctx.request.body.email || '',
7+
password = ctx.request.body.password || '';
8+
if (email === 'admin@example.com' && password === '123456') {
9+
console.log('signin ok!');
10+
ctx.render('signin-ok.html', {
11+
name: 'Mr Node',
12+
last: new Date(),
13+
});
14+
} else {
15+
console.log('signin failed!');
16+
ctx.render('signin-failed.html');
17+
}
18+
}
19+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "view-koa",
3+
"version": "1.0.0",
4+
"description": "koa 2 example with nunjucks as view",
5+
"main": "start.js",
6+
"scripts": {
7+
"start": "node start.js"
8+
},
9+
"keywords": [
10+
"koa",
11+
"async",
12+
"nunjucks"
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+
"nunjucks": "2.4.2",
29+
"mime": "1.3.4",
30+
"mz": "2.4.0"
31+
}
32+
}
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');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path');
2+
const mime = require('mime');
3+
const fs = require('mz/fs');
4+
5+
function staticFiles(url, dir) {
6+
return async (ctx, next) => {
7+
let rpath = ctx.request.path;
8+
if (rpath.startsWith(url)) {
9+
let fp = path.join(dir, rpath.substring(url.length));
10+
if (await fs.exists(fp)) {
11+
ctx.response.type = mime.lookup(rpath);
12+
ctx.response.body = await fs.readFile(fp);
13+
} else {
14+
ctx.response.status = 404;
15+
}
16+
} else {
17+
await next();
18+
}
19+
};
20+
}
21+
22+
module.exports = staticFiles;

0 commit comments

Comments
 (0)