zoukankan      html  css  js  c++  java
  • koa2

    提示工具:node调试工具:chrome://inspect 浏览器打开(先跑koa2)

    npm安装

    npm i koa --save
    

     router安装

    npm install koa-router --save
    

     get请求获取请求头参数

    router.get('/listData', async (ctx, next) =>{  //ctx 包含了request 和response
        console.log(ctx.query);  //{ aid: '123' }       获取的是对象   用的最多的方式  **推荐
        console.log(ctx.querystring);  //aid=123&name=zhangsan      获取的是一个字符串
        console.log(ctx.url);   //获取url地址
        let obj = {
            name:'guangfa',
            age:18
        }
        ctx.body = obj //ctx.body返回数据
    })
    

     post需要用到Koa 中koa-bodyparser中间件

     1.安装 npm install --save koa-bodyparser

     2.app.js引入使用他(注意要在router前引入和使用)

    const bodyParser = require('koa-bodyparser'); //post请求的中间件
    app.use(bodyParser());
    

     

     app.js 
    1
    const Koa = require('koa') 2 const bodyParser = require('koa-bodyparser'); //post请求的中间件 3 4 const app = new Koa() 5 6 app.use(bodyParser()); 7 8 let router = require('./router/allRoute') //引入路由 9 10 app.use(router.routes()) //使用路由和启用 11 app.use(router.allowedMethods()); 12 13 14 app.listen(3000,()=>{ 15 console.log('服务已经启动 通过http://192.168.0.4:3000/') 16 })

     

    aiiRouter.js
    1
    //总路由配置 2 3 const router = require('koa-router')(); 4 5 router.get('/listData', async (ctx, next) =>{ //ctx 包含了request 和response 6 console.log(ctx.query); //{ aid: '123' } 获取的是对象 用的最多的方式 **推荐 7 console.log(ctx.querystring); //aid=123&name=zhangsan 获取的是一个字符串 8 console.log(ctx.url); //获取url地址 9 let obj = { 10 name:'guangfa', 11 age:18 12 } 13 ctx.body = obj //ctx.body返回数据 14 }) 15 16 router.post('/login', async (ctx, next) =>{ 17 //ctx.request.body 获取post请求的参数 18 ctx.body = ctx.request.body 19 }) 20 module.exports = router; //导出路由

     mongodb可视化管理工具https://www.mongodb.com/download-center/compass?jmp=hero

    koa2配置跨域 , 拷贝放入app.js 在使用路由前面配置

    app.use(async (ctx, next)=> {
        ctx.set('Access-Control-Allow-Origin', '*');
        ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
        ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
        if (ctx.method == 'OPTIONS') {
          ctx.body = 200; 
        } else {
          await next();
        }
      });
    

      

  • 相关阅读:
    metal的gpu query
    体积雾 global fog unity 及改进
    hdr rt format对颜色的影响
    unity deferred lighting
    unity linear space时 photoshop blend的正确设置
    unity linear work flow
    一些数据 bandwidth之类
    deferred rendering with msaa
    unity 显示mipmaplevel
    【转】在C#中使用SendMessage
  • 原文地址:https://www.cnblogs.com/gfweb/p/10979592.html
Copyright © 2011-2022 走看看