網站首頁 編程語言 正文
使用axios以及express框架進行數據傳輸
react腳手架中src文件配置如下:
App.js:
設置兩個按鈕,點擊第一個獲取學生數據,點擊第二個獲取汽車數據,值得注意的是這兩個數據源在不同的服務器中
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}>點我獲取學生數據</button>
<button onClick={this.getCarData}>點我獲取學生數據</button>
</div>
)
}
}
index.js:
腳手架入口文件
// 入口文件
//引入react核心庫
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:
服務器1的代碼包含學生數據
const express = require('express')
const app = express()
app.use((request, response, next) => {
console.log("有人請求服務器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("服務器1啟動成功,地址為http://localhost:5000/students")
})
server2.js
服務器2的內容,包含汽車的數據
const express = require('express')
const app = express()
app.use((request, response, next) => {
console.log("有人請求服務器2");
next();
})
app.get('/cars', (request, response) => {
const cars = [
{id:"001", name:"寶馬", price:18},
{id:"002", name:"奔馳", price:18},
{id:"003", name:"保時捷", price:8},
]
response.send(cars)
})
app.listen(5001, (err) => {
if(!err)console.log("服務器2啟動成功,地址為http://localhost:5001/cars")
})
setupProxy.js:
分別配置不同的代理(b站尚硅谷視頻里那種運行不出來,版本更新了,下面這種目前可以跑出來)
const { createProxyMiddleware } = require('http-proxy-middleware')
module.exports = function (app) {
app.use(
createProxyMiddleware('/api1', {
//api1是需要轉發的請求(所有帶有/api1前綴的請求都會轉發給5000)
target: 'http://localhost:5000', //配置轉發目標地址(能返回數據的服務器地址)
changeOrigin: true, //控制服務器接收到的請求頭中host字段的值
pathRewrite: { '^/api1': '' }, //去除請求前綴,保證交給后臺服務器的是正常請求地址(必須配置)
}),
createProxyMiddleware('/api2', {
target: 'http://localhost:5001',
changeOrigin: true,
pathRewrite: { '^/api2': '' },
})
)
}
運行
啟動服務器1:
node server1.js
啟動服務器2:
node server2.js
啟動腳手架:
npm start
訪問頁面
點擊第一個按鈕:
點擊第二個按鈕:
原文鏈接:https://blog.csdn.net/qq_52785473/article/details/126492201
相關推薦
- 2024-02-16 SpringBoot 全局異常處理
- 2022-10-02 C/C++寬窄字符轉換與輸出的多種實現方法_C 語言
- 2022-12-12 flutter?Bloc?更新后事件同步與異步詳解_Android
- 2022-10-16 QT編寫tcp通信工具(Server端)_C 語言
- 2022-06-07 Selenium瀏覽器自動化如何上傳文件_python
- 2022-11-14 .NET?Core?Web?APi類庫內嵌運行的方法_實用技巧
- 2022-07-19 C++分別使用std::chrono和clock()計算時間間隔
- 2022-04-15 Python爬蟲之requests庫基本介紹_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支