post与get,关于终端

//引入类
const Koa=require('koa');
const koaBody = require('koa-body');
const Router=require('koa-router');

 

// const koaBody= require('koa');
//引入内部方法或属性
// const {Koa(方法或属性名)}=require('koa');

 

//创建对象
const app=new Koa();
app.use(koaBody());
const router=new Router();
 
//地址:localhost:3004
// app.listen(3004);//监听端口号3004

 

router.get("/",async (ctx)=>{
    //url参数 ctx.query
    console.log(ctx.url);
    console.log(ctx.query);
    // console.log(ctx.querystring);
})
// postman 测试后台接口
router.post("/a",async ctx=>{
    console.log(ctx.url);
    console.log(ctx.request.body);
    ctx.body="请求成功";
});

 

// 调用router.routes()来组装匹配好的路由,返回一个合并好的中间件
// 调用router.allowedMethods()获得一个中间件,当发送了不符合的请求时,会返回 `405 Method Not Allowed` 或 `501 Not Implemented`
app.use(router.routes());
app.use(router.allowedMethods({
    // throw: true, // 抛出错误,代替设置响应头状态
    // notImplemented: () => '不支持当前请求所需要的功能',
    // methodNotAllowed: () => '不支持的请求方式'
}));
app.listen(3004,()=>{
    console.log("http://localhost:3004")
})
 
POST http://localhost:3004/a
Content-Type: application/json

{
    "id":1000,
    "name":"张三"
}
 

 

 发送请求

posted @ 2021-11-26 12:19  610鸭  阅读(46)  评论(0)    收藏  举报