美文网首页
十.第10节:Koa-router中间件(3)参数

十.第10节:Koa-router中间件(3)参数

作者: qqqc | 来源:发表于2018-02-13 17:36 被阅读0次

我们先来作一个最简单的路由。

const Koa = require('koa');
const Router = require('koa-router');
 
const app = new Koa();
const router = new Router();
 
router.get('/', function (ctx, next) {
    
 
    ctx.body="Hello JSPang";
});
 
app
  .use(router.routes())
  .use(router.allowedMethods());
  app.listen(3000,()=>{
      console.log('starting at port 3000');
  });

然后我们回忆一下第三节课中学到的接收参数的方法,这里我们使用最易用的方法ctx.query来进行接收,修改为下面代码的第6行,这样就可以轻松接收get参数。

const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
router.get('/', function (ctx, next) {
    ctx.body=ctx.query;
});
app
  .use(router.routes())
  .use(router.allowedMethods());
  app.listen(3000,()=>{
      console.log('starting at port 3000');
  });

相关文章

  • koa

    koa 学习 中间件 koa-router koa-router 获取get/post请求参数 koa-bodyp...

  • koa-router中间件

    1 .安装koa-router中间件 2 .最基本的koa-router的写法 3 .层级

  • 十.第10节:Koa-router中间件(3)参数

    我们先来作一个最简单的路由。 然后我们回忆一下第三节课中学到的接收参数的方法,这里我们使用最易用的方法ctx.qu...

  • node 中间件 koa-router

    koa-router中间件的基本使用 1.安装npm install --save koa-router 2.实例...

  • 2.koa路由

    原生koa路由 index.js index.html koa-router中间件

  • 玩转Koa之koa-router原理解析

    本文将要分析的是经常用到的路由中间件 -- koa-router,详细的介绍了koa-router概述和实现,写的...

  • koa 常用模块

    koa-router koa路由中间件https://github.com/alexmingoia/koa-rou...

  • 知识点总结

    Koa2中间件 koa(面向node.js的表达式HTTP中间件框架)、koa-router(路由中间件)、koa...

  • Koa2学习系列03-路由koa-router——MVC 中重要

    路由 koa-router 上一节我们学习了中间件的基本概念,本节主要带大家学习下 koa-router 路由中间...

  • koa-router allowedMethods

    koa-router 是koa框架的一个路由处理级别的中间件 koa-router可以说是koa必须用上的一个库了...

网友评论

      本文标题:十.第10节:Koa-router中间件(3)参数

      本文链接:https://www.haomeiwen.com/subject/jcajtftx.html