koa2系列教程,持续更新koa2系列教程:koa2应用初探koa2系列教程:koa2处理静态文件koa2系列教程:koa2使用模板引擎koa2系列教程:koa2路由控制中间件koa2系列教程:全面的koa2搭建登录注册页面看这里本文使用koa-router控制路由版本:注意版本目录结构:1.编辑index.jsconstkoa=require('koa')constRouter=require('koa-router')constapp=newKoa()//子路由1consthome=newRouter()home.get('/',async(ctx)=>{ctx.body="homepages"})//子路由2constpage=newRouter()page.get('/404',async(ctx)=>{ctx.body='404pages'})constlogin=newRouter()login.get('/',async(ctx)=>{ctx.body='loginpages'})//加载所有子路由letrouter=newRouter()router.use('/',home.routes(),home.allowedMethods())router.use('/page',page.routes(),page.allowedMethods())router.use('/login',login.routes(),login.allowedMethods())//加载路由中间件app.use(router.routes()).use(router.allowedMethods())app.listen(3000,()=>{console.log('localhost:3000')})2.启动服务,打开浏览器节点index.js访问:localhost:3000,localhost;3000/login,localhost:3000/page/404都是可见的结果koa-router的其他API源码地址:https://github.com/alexmingoi...router.get('/',(ctx,next)=>{ctx.body='HelloWorld!';}).post('/users',(ctx,next)=>{//...}).put('/users/:id',(ctx,next)=>{//...})。del('/users/:id',(ctx,next)=>{//...}).all('/users/:id',(ctx,next)=>{//...});后记koa相关的路由控制中间件有很多。这取决于您的选择。不妨关注一下
