Skip to content

Commit

Permalink
docs(examples): add egg.js examples
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 25, 2018
1 parent 8c3261a commit d8c1b5b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
19 changes: 19 additions & 0 deletions examples/framework/helloworld.egg/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const options = {
baseDir: __dirname,
plugin: null
};

const { EggCore: Application } = require('egg-core');
const app = new Application(options);

app.loader.loadConfig();
app.loader.loadController();


require('./app/router')(app);


module.exports = app;

13 changes: 13 additions & 0 deletions examples/framework/helloworld.egg/app/controller/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const Controller = require('egg').Controller;

class FooController extends Controller {
async render() {
const ctx = this.ctx;

ctx.body = 'Hello foo';
}
}

module.exports = FooController;
12 changes: 12 additions & 0 deletions examples/framework/helloworld.egg/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
async render() {
const ctx = this.ctx;
ctx.body = 'Hello Egg';
}
}

module.exports = HomeController;
8 changes: 8 additions & 0 deletions examples/framework/helloworld.egg/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = app => {
app.router.get('/egg/', app.controller.home.render);
app.router.get('/egg/home', app.controller.home.render);
app.router.get('/egg/foo', app.controller.foo.render);
};

7 changes: 7 additions & 0 deletions examples/framework/helloworld.egg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "helloworld.egg",
"dependencies": {
"egg": "^1.10.1"
},
"private": true
}
8 changes: 5 additions & 3 deletions examples/framework/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"version": "0.0.1",
"dependencies": {
"koa": "*",
"egg": "^2.9.1",
"express": "*",
"hapi": "*"
"hapi": "*",
"koa": "*"
},
"engines": {
"node": ">= 8.0.0"
}
},
"private": true
}
18 changes: 14 additions & 4 deletions examples/framework/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

// 定义一个路由表
const map = {
hapi: './hapi.js', // http://127.0.0.1/hapi
express: './express.js', // http://127.0.0.1/express
koa: './koa.js', // http://127.0.0.1/koa
default: './helloworld.js' // http://127.0.0.1/other
// http://127.0.0.1/egg
egg: './helloworld.egg/app.js',

// http://127.0.0.1/hapi
hapi: './hapi.js',

// http://127.0.0.1/express
express: './express.js',

// http://127.0.0.1/koa
koa: './koa.js',

// http://127.0.0.1/other
default: './helloworld.js'
};

// 路由:起个名字
Expand Down

0 comments on commit d8c1b5b

Please sign in to comment.