github:https://github.com/ikcamp/koa2-tutorial/
npm i koa-bodyparser -S
post请求数据放在ctx.body中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| const Koa = require('koa') const router = require('koa-router')() const bodyParser = require('koa-bodyparser') const app = new Koa()
app.use(bodyParser())
router.get('/', async(ctx, next) => { ctx.response.body = `<h1>index page</h1>` })
router.get('/home', async(ctx, next) => { console.log(ctx.request.query) console.log(ctx.request.querystring) ctx.response.body = '<h1>HOME page</h1>' })
router.get('/home/:id/:name', async(ctx, next)=>{ console.log(ctx.params) ctx.response.body = '<h1>HOME page /:id/:name</h1>' })
router.get('/404', async(ctx, next) => { ctx.response.body = '<h1>404 Not Found</h1>' })
app.use(router.routes())
app.listen(3000, () => { console.log('server is running at http://localhost:3000') })
|
koa controller + nunjucks
npm i koa-nunjucks-2 -S
controller/home.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| const HomeService = require('../service/home')
module.exports = { index: async (ctx, next) => { ctx.response.body = `<h1>index page</h1>` }, home: async (ctx, next) => { console.log(ctx.request.query) console.log(ctx.request.querystring) ctx.response.body = '<h1>HOME page</h1>' }, homeParams: async (ctx, next) => { console.log(ctx.params) ctx.response.body = '<h1>HOME page /:id/:name</h1>' }, login: async (ctx, next) => { await ctx.render('home/login', { btnName: 'GoGoGo' }) },, register: async (ctx, next) => { let { name, password } = ctx.request.body let data = await HomeService.register(name, password);
ctx.response.body = data;
} }
|
router.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const router = require('koa-router')() const HomeController = require('./controller/home') module.exports = (app) => { router.get('/', HomeController.index)
router.get('/home', HomeController.home)
router.get('/home/:id/:name', HomeController.homeParams)
router.get('/user', HomeController.login)
router.post('/user/register', HomeController.register)
app.use(router.routes()) .use(router.allowedMethods()) }
|
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const Koa = require('koa') // const router = require('koa-router')() const bodyParser = require('koa-bodyparser') const app = new Koa() const router = require('./router') const nunjucks = require('koa-nunjucks-2')
app.use(bodyParser())
app.use(nunjucks({ ext: 'html', path: path.join(__dirname, 'views'), nunjucksConfig: { trimBlocks: true } }))
router(app);
app.listen(3000, () => { console.log('server is running at http://localhost:3000') })
|
views/home/login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <!DOCTYPE html> <html lang="en">
<head> <title></title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head>
<body> <form action="/user/register" method="post"> <input name="name" type="text" placeholder="请输入用户名:ikcamp" /> <br /> <input name="password" type="text" placeholder="请输入密码:123456" /> <br /> <button>{{btnName}}</button> </form> </body>
</html>
|
静态资源
npm i koa-static -S