1,原生路由
网站一般都有多个页面,通过 【ctx.request.path】获取用户请求的路径,由此实现简单的路由
const Koa = require('koa')
const app = new Koa()
const main = ctx => {
if(ctx.request.path !== '/') {
ctx.response.type = 'html'
ctx.response.body = '<a href="/">To Index</a>'
} else {
ctx.response.body = 'Hello World'
}
}
app.use(main)
app.listen(3000)
console.log('server running at localhost:3000')
2,koa-route 模块
const Koa = require('koa')
const app = new Koa()