[微前端实战]---038 请求数据

后端服务-请求数据


你将学到

  1. koa2跨域配置,
  2. koa2(get/post) 及其参数获取
  3. koa2 路由中间件配置

一. 添加启动命令

build/run.js

...

const filePath = {
...
  // 添加启动service命令
+  service: path.join(__dirname, '../service')
}
...
runChild()

新建router/vue2.js

1.1 KOA get请求
const router = require('koa-router')()

router.prefix('/vue2') // 添加前缀 http://localhost:3000/vue2

router.get('/car/list', function (ctx, next) {
  // ctx.request.query   // 获取Get请求参数
  
  ctx.body = [
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
    {
      img: 'https://youjia-image.cdn.bcebos.com/modelImage/f9037020cac55360e436c2a4add6cef9/16593358938844266694.jpg@!1200_width',
      name: '沃尔沃'
    },
  ]
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

module.exports = router
async getCarList() {
      const res = await axios.get('http://localhost:3000/vue2/car/list?a=1&b=2')
      this.carList = res.data
}

新建router/react17.js

1.2 KOA post请求
const router = require('koa-router')()

router.prefix('/react17') // 添加前缀 http://localhost:3000/react17

router.post('/login', function (ctx, next) {
  // ctx.request.body   // 获取POST请求参数
  ctx.body = 'this is respose' 
})



module.exports = router
  axios.post('http://localhost:3000/react17/login', {
      a: 1,
      b: 2,
    })
      .then(res => {
        console.log('登录成功')
    })

app.js 引入中间件和路由

...
// 注册koa中间件
+ const cors = require('koa-cors');   // 解决跨域


// 引入vue2的接口内容
+ const vue2 = require('./routes/vue2')
// 引入react17的接口内容
+ const react17 = require('./routes/react17')

...
+ app.use(cors());


// 注册routes
...
+ app.use(vue2.routes(), vue2.allowedMethods())
+ app.use(react17.routes(), react17.allowedMethods())


module.exports = app

注意要注册中间件, 在注册路由文件, 然后打开浏览器请求服务.

跨域问题, 配置代理服务,见结尾.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d7IaP6Gy-1661603482397)(img/image-20220827193548448.png)]

再打开

http://localhost:8080/#/energy, 发起get请求

在这里插入图片描述

http://localhost:8083/#/login, 发起post请求

在这里插入图片描述

后端服务-请求数据

回顾.

  1. 将服务设置为可跨域状态koa2-cors插件实现

  2. app.js 引入插件, router文件 注册路由

  3. ctx.request.query // 获取get请求参数

  4. ctx.request.body // 获取post请求参数

  5. axios.get /axios.post

下一章, 将对项目进行微前端改造

配置中间件.

配置中间件

/service

$	cd service
$	npm install koa-cors --save

主要通过koa-cors插件来处理,共3个步骤:

①安装:

npm install koa-cors --save

②引入:

const cors = require('koa-cors');   // 解决跨域

③注册:

app.use(cors());

ps:注意,要把 app.use(cors()); 放在路由的前面,坑已踩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值