安装koa和koa-router
npm install koa --save
npm install koa-router --save
var koa = require('koa');
var Router = require('koa-router');
var app = new koa();
var router = new Router();
router.get('/', async (ctx) => {
ctx.body = "首页";
})
router..get('/news', async (ctx) => {
ctx.body = "新闻";
})
// 获取get传值
router.get('/newsContent', async (ctx) => {
// http://localhost:3000/newsContent?id=3
console.log(ctx.query.id) //获取get传值 3
ctx.body = "新闻详情";
})
// 配置动态路由
router.get('/newsCtx/:id', async (ctx) => {
// http://localhost:3000/newsCtx/koa
console.log(ctx.params) //获取动态路由返回值
ctx.body = "新闻详情";
})
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(3000);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有