Skip to content

Commit d8c1b5b

Browse files
committed
docs(examples): add egg.js examples
1 parent 8c3261a commit d8c1b5b

File tree

7 files changed

+78
-7
lines changed

7 files changed

+78
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const options = {
4+
baseDir: __dirname,
5+
plugin: null
6+
};
7+
8+
const { EggCore: Application } = require('egg-core');
9+
const app = new Application(options);
10+
11+
app.loader.loadConfig();
12+
app.loader.loadController();
13+
14+
15+
require('./app/router')(app);
16+
17+
18+
module.exports = app;
19+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const Controller = require('egg').Controller;
4+
5+
class FooController extends Controller {
6+
async render() {
7+
const ctx = this.ctx;
8+
9+
ctx.body = 'Hello foo';
10+
}
11+
}
12+
13+
module.exports = FooController;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const Controller = require('egg').Controller;
4+
5+
class HomeController extends Controller {
6+
async render() {
7+
const ctx = this.ctx;
8+
ctx.body = 'Hello Egg';
9+
}
10+
}
11+
12+
module.exports = HomeController;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
app.router.get('/egg/', app.controller.home.render);
5+
app.router.get('/egg/home', app.controller.home.render);
6+
app.router.get('/egg/foo', app.controller.foo.render);
7+
};
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "helloworld.egg",
3+
"dependencies": {
4+
"egg": "^1.10.1"
5+
},
6+
"private": true
7+
}

examples/framework/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"version": "0.0.1",
33
"dependencies": {
4-
"koa": "*",
4+
"egg": "^2.9.1",
55
"express": "*",
6-
"hapi": "*"
6+
"hapi": "*",
7+
"koa": "*"
78
},
89
"engines": {
910
"node": ">= 8.0.0"
10-
}
11+
},
12+
"private": true
1113
}

examples/framework/router.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
// 定义一个路由表
44
const map = {
5-
hapi: './hapi.js', // http://127.0.0.1/hapi
6-
express: './express.js', // http://127.0.0.1/express
7-
koa: './koa.js', // http://127.0.0.1/koa
8-
default: './helloworld.js' // http://127.0.0.1/other
5+
// http://127.0.0.1/egg
6+
egg: './helloworld.egg/app.js',
7+
8+
// http://127.0.0.1/hapi
9+
hapi: './hapi.js',
10+
11+
// http://127.0.0.1/express
12+
express: './express.js',
13+
14+
// http://127.0.0.1/koa
15+
koa: './koa.js',
16+
17+
// http://127.0.0.1/other
18+
default: './helloworld.js'
919
};
1020

1121
// 路由:起个名字

0 commit comments

Comments
 (0)