koa - 路由

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()

猜你喜欢

转载自blog.csdn.net/M_wolf/article/details/81590611