koa2入门学习(2)-路由GET请求
1.安装依赖 npm i koa-router
2.修改app.js:
const Koa = require('koa') const router = require('koa-router')() // 这里要注意,返回的是函数 const app = new Koa() app.use(async(ctx,next) => { await next() }) router.get('/hello/:name', async (ctx,next) => { var name = ctx.params.name ctx.response.body = `<h3> hello ${name}</h3>` }) router.get('/', async (ctx,next) => { ctx.response.body = `<h3> index <h3>` }) app.use(router.routes()) app.listen(3000) console.log('app started at port 3000...')
3.效果如下: