亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React配置多個(gè)代理實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求返回問(wèn)題

 更新時(shí)間:2022年08月25日 10:58:09   作者:Icy?Hunter  
這篇文章主要介紹了React之配置多個(gè)代理實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求返回問(wèn)題,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

使用axios以及express框架進(jìn)行數(shù)據(jù)傳輸

react腳手架中src文件配置如下:

App.js:

設(shè)置兩個(gè)按鈕,點(diǎn)擊第一個(gè)獲取學(xué)生數(shù)據(jù),點(diǎn)擊第二個(gè)獲取汽車數(shù)據(jù),值得注意的是這兩個(gè)數(shù)據(jù)源在不同的服務(wù)器中

import React, { Component } from 'react'
import axios from "axios"
export default class App extends Component {
	getStudentData = () => {
		axios.get("http://localhost:3000/api1/students").then(
			response => {console.log("成功了", response.data);},
			error => {console.log("失敗了", error);}
		)
	}
	getCarData = () => {
		axios.get("http://localhost:3000/api2/cars").then(
			response => {console.log("成功了", response.data);},
			error => {console.log("失敗了", error);}
		)
	}
  render() {
	return (
	  <div>
		<button onClick={this.getStudentData}>點(diǎn)我獲取學(xué)生數(shù)據(jù)</button>
		<button onClick={this.getCarData}>點(diǎn)我獲取學(xué)生數(shù)據(jù)</button>
	  </div>
	)
  }
}

index.js:

腳手架入口文件

// 入口文件
//引入react核心庫(kù)
import React from 'react';
//引入ReactDOM
import ReactDOM from 'react-dom/client'
//引入App組件
import App from "./App"
// import ReactDOM from 'react-dom/client'
const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(<App/>)

server1.js:

服務(wù)器1的代碼包含學(xué)生數(shù)據(jù)

const express = require('express')
const app = express()
app.use((request, response, next) => {
    console.log("有人請(qǐng)求服務(wù)器1");
    next();
})
app.get('/students', (request, response) => {
    const students = [
        {id:"001", name:"tom", age:18},
        {id:"002", name:"jerry", age:18},
        {id:"003", name:"tony", age:8},
    ]
    response.send(students)
})

app.listen(5000, (err) => {
    if(!err)console.log("服務(wù)器1啟動(dòng)成功,地址為http://localhost:5000/students")
})

server2.js

服務(wù)器2的內(nèi)容,包含汽車的數(shù)據(jù)

const express = require('express')
const app = express()
app.use((request, response, next) => {
    console.log("有人請(qǐng)求服務(wù)器2");
    next();
})
app.get('/cars', (request, response) => {
    const cars = [
        {id:"001", name:"寶馬", price:18},
        {id:"002", name:"奔馳", price:18},
        {id:"003", name:"保時(shí)捷", price:8},
    ]
    response.send(cars)
})

app.listen(5001, (err) => {
    if(!err)console.log("服務(wù)器2啟動(dòng)成功,地址為http://localhost:5001/cars")
})

setupProxy.js:

分別配置不同的代理(b站尚硅谷視頻里那種運(yùn)行不出來(lái),版本更新了,下面這種目前可以跑出來(lái))

const { createProxyMiddleware } = require('http-proxy-middleware')
module.exports = function (app) {
 app.use(
  createProxyMiddleware('/api1', {
   //api1是需要轉(zhuǎn)發(fā)的請(qǐng)求(所有帶有/api1前綴的請(qǐng)求都會(huì)轉(zhuǎn)發(fā)給5000)
   target: 'http://localhost:5000', //配置轉(zhuǎn)發(fā)目標(biāo)地址(能返回?cái)?shù)據(jù)的服務(wù)器地址)
   changeOrigin: true, //控制服務(wù)器接收到的請(qǐng)求頭中host字段的值
   pathRewrite: { '^/api1': '' }, //去除請(qǐng)求前綴,保證交給后臺(tái)服務(wù)器的是正常請(qǐng)求地址(必須配置)

  }),
  createProxyMiddleware('/api2', {
   target: 'http://localhost:5001',
   changeOrigin: true,
   pathRewrite: { '^/api2': '' },
  })
 )
}

運(yùn)行

啟動(dòng)服務(wù)器1:

node server1.js

啟動(dòng)服務(wù)器2:

node server2.js

啟動(dòng)腳手架:

npm start

訪問(wèn)頁(yè)面

在這里插入圖片描述

點(diǎn)擊第一個(gè)按鈕:

在這里插入圖片描述

點(diǎn)擊第二個(gè)按鈕:

在這里插入圖片描述

到此這篇關(guān)于React配置多個(gè)代理實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求返回的文章就介紹到這了,更多相關(guān)React配置多個(gè)代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論