query
const Koa = require("koa");
const router = require("koa-router")();
const app = new Koa();
// http://localhost:8080/?a=1&b=2
router.get("/", async ctx => {
console.log(ctx.query); //{a:1,b:2}
console.log(ctx.querystring); // a=1&b=2
console.log(ctx.request.query); //{a:1,b:2}
console.log(ctx.request.querystring); //a=1&b=2
ctx.body = "home";
});
app
.use(router.routes()) // 启动路由
.use(router.allowedMethods());
app.listen(8080);
params
const Koa = require("koa");
const router = require("koa-router")();
const app = new Koa();
// http://localhost:8080/news/aaa
router.get("/news/:id", async ctx => {
console.log(ctx.params); //{id:'aaa'}
ctx.body = "news";
});
app
.use(router.routes()) // 启动路由
.use(router.allowedMethods());
app.listen(8080);
和vue是一模一样的呀
网友评论