koa 基礎(四)get 傳值 以及獲取 get 傳值

1.get 傳值 以及獲取 get 傳值javascript

app.jsjava

/**
 * get 傳值 以及獲取 get 傳值
 */
// 引入模塊
const Koa = require('koa');
const router = require('koa-router')(); /*引入是實例化路由 推薦*/

// 實例化
let app = new Koa();

router.get('/', async (ctx) => {
  ctx.body = '首頁';
})

router.get('/news', async (ctx) => {
  ctx.body = '新聞列表頁面';
})

router.get('/newscontent', async (ctx) => {
  // 從 ctx 中讀取get傳值
  console.log(ctx.query); // 獲取的是對象
  console.log(ctx.querystring); // 獲取的是一個字符串
  console.log(ctx.url); // 獲取url地址

  // ctx裏面的 request 裏面獲取 get 傳值
  console.log(ctx.request.url);
  console.log(ctx.request.query);
  console.log(ctx.request.querystring);

  ctx.body = '新聞詳情';
})

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000);

.app

相關文章
相關標籤/搜索