koa-router简单使用

目录

koa-router 实现路由


const Koa = require('koa');
/**
 * 调用方式
 * */
const Router = require('koa-router')();
const App = new Koa();

Router.get('/',(ctx,next)=>{
    ctx.body = 'Hello koa';
})

Router.get('/news',(ctx,next)=>{
    ctx.body = '新闻App'
})
/**
 * 启动路由
 * */
App.use(Router.routes());
/**
 * 我们可以看到router.allowedMethods()用在了路由匹配router.routes()之后,
 * 所以在当所有路由中间件最后调用.此时根据ctx.status设置response响应头
 * */
App.use(Router.allowedMethods());

App.listen(3000,()=>{
    console.log('quick start at port 3000')
})
发布了31 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38694034/article/details/105247180