React配置多個(gè)代理實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求返回問題
使用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)行不出來,版本更新了,下面這種目前可以跑出來)
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
訪問頁(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)文章
ReactHook使用useState更新變量后,如何拿到變量更新后的值
這篇文章主要介紹了ReactHook使用useState更新變量后,如何拿到變量更新后的值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
react-router browserHistory刷新頁(yè)面404問題解決方法
本篇文章主要介紹了react-router browserHistory刷新頁(yè)面404問題解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12
React?Hooks項(xiàng)目實(shí)戰(zhàn)
React?Hooks是React?16.8版本引入的新特性,它使得在函數(shù)組件中也能夠使用狀態(tài)(state)和其他React特性,本文就來詳細(xì)介紹一下React?Hooks項(xiàng)目實(shí)戰(zhàn),感興趣的可以了解一下2023-11-11
React中使用Workbox進(jìn)行預(yù)緩存的實(shí)現(xiàn)代碼
Workbox是Google Chrome團(tuán)隊(duì)推出的一套 PWA 的解決方案,這套解決方案當(dāng)中包含了核心庫(kù)和構(gòu)建工具,因此我們可以利用Workbox實(shí)現(xiàn)Service Worker的快速開發(fā),本文小編給大家介紹了React中使用Workbox進(jìn)行預(yù)緩存的實(shí)現(xiàn),需要的朋友可以參考下2023-11-11
React videojs 實(shí)現(xiàn)自定義組件(視頻畫質(zhì)/清晰度切換) 的操作代碼
最近使用videojs作為視頻處理第三方庫(kù),用來對(duì)接m3u8視頻類型,這里總結(jié)一下自定義組件遇到的問題及實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2023-08-08
JS中使用react-tooltip插件實(shí)現(xiàn)鼠標(biāo)懸浮顯示框
前段時(shí)間遇到的一個(gè)需求,要求鼠標(biāo)懸停顯示使用描述, 用到了react-tooltip插件,今天寫一個(gè)總結(jié),感興趣的朋友跟隨小編一起看看吧2019-05-05
React 模塊聯(lián)邦多模塊項(xiàng)目實(shí)戰(zhàn)詳解
這篇文章主要介紹了React 模塊聯(lián)邦多模塊項(xiàng)目實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解
這篇文章主要為大家介紹了阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

