網(wǎng)站首頁 編程語言 正文
使用axios以及express框架進行數(shù)據(jù)傳輸
react腳手架中src文件配置如下:
App.js:
設(shè)置兩個按鈕,點擊第一個獲取學(xué)生數(shù)據(jù),點擊第二個獲取汽車數(shù)據(jù),值得注意的是這兩個數(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}>點我獲取學(xué)生數(shù)據(jù)</button>
<button onClick={this.getCarData}>點我獲取學(xué)生數(shù)據(jù)</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:
服務(wù)器1的代碼包含學(xué)生數(shù)據(jù)
const express = require('express')
const app = express()
app.use((request, response, next) => {
console.log("有人請求服務(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啟動成功,地址為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("有人請求服務(wù)器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("服務(wù)器2啟動成功,地址為http://localhost:5001/cars")
})
setupProxy.js:
分別配置不同的代理(b站尚硅谷視頻里那種運行不出來,版本更新了,下面這種目前可以跑出來)
const { createProxyMiddleware } = require('http-proxy-middleware')
module.exports = function (app) {
app.use(
createProxyMiddleware('/api1', {
//api1是需要轉(zhuǎn)發(fā)的請求(所有帶有/api1前綴的請求都會轉(zhuǎn)發(fā)給5000)
target: 'http://localhost:5000', //配置轉(zhuǎn)發(fā)目標(biāo)地址(能返回數(shù)據(jù)的服務(wù)器地址)
changeOrigin: true, //控制服務(wù)器接收到的請求頭中host字段的值
pathRewrite: { '^/api1': '' }, //去除請求前綴,保證交給后臺服務(wù)器的是正常請求地址(必須配置)
}),
createProxyMiddleware('/api2', {
target: 'http://localhost:5001',
changeOrigin: true,
pathRewrite: { '^/api2': '' },
})
)
}
運行
啟動服務(wù)器1:
node server1.js
啟動服務(wù)器2:
node server2.js
啟動腳手架:
npm start
訪問頁面
點擊第一個按鈕:
點擊第二個按鈕:
原文鏈接:https://blog.csdn.net/qq_52785473/article/details/126492201
相關(guān)推薦
- 2022-04-24 Redis三種特殊數(shù)據(jù)類型的具體使用_Redis
- 2023-02-07 C#實現(xiàn)自定義屏保的示例代碼_C#教程
- 2023-12-18 Path環(huán)境變量點編輯無法展開
- 2022-07-12 C++重載運算符時函數(shù)類型的選擇與類的類型轉(zhuǎn)換
- 2022-01-31 RuntimeError:Given input size:(256x1x1). Calculate
- 2022-11-10 Python3中字符串的常用操作方法及查找方法_python
- 2022-05-18 ASP.NET?MVC過濾器執(zhí)行順序介紹_實用技巧
- 2022-04-28 C#網(wǎng)絡(luò)編程中常用特性介紹_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支