目录:
- const express = require('express')
- const app = express()
-
- app.use((request,response,next)=>{
- console.log('有人请求服务器1了');
- console.log('请求来自于',request.get('Host'));
- console.log('请求的地址',request.url);
- next()
- })
-
- app.get('/students',(request,response)=>{
- const students = [
- {id:'001',name:'tom',age:18},
- {id:'002',name:'jerry',age:19},
- {id:'003',name:'tony',age:120},
- ]
- response.send(students)
- })
-
- app.listen(5000,(err)=>{
- if(!err) console.log('服务器1启动成功了,请求学生信息地址为:http://localhost:5000/students');
- })
- import React, {Component} from 'react';
- import axios from "axios";
-
- class App extends Component {
- getStudentData = () => {
- axios.get('http://localhost:5000/students').then(
- response => {
- console.log('成功了', response.data);
- },
- error => {
- console.log('失败了', error);
- }
- )
- }
-
- render() {
- return (
-
-
-
- );
- }
- }
-
- export default App;
- {
- "name": "20231003",
- "version": "0.1.0",
- "private": true,
- "dependencies": {
- "@testing-library/jest-dom": "^5.17.0",
- "@testing-library/react": "^13.4.0",
- "@testing-library/user-event": "^13.5.0",
- "axios": "^1.5.1",
- "nanoid": "^5.0.1",
- "prop-types": "^15.8.1",
- "react": "^18.2.0",
- "react-dom": "^18.2.0",
- "react-scripts": "5.0.1",
- "web-vitals": "^2.1.4"
- },
- "scripts": {
- "start": "react-scripts start",
- "build": "react-scripts build",
- "test": "react-scripts test",
- "eject": "react-scripts eject"
- },
- "eslintConfig": {
- "extends": [
- "react-app",
- "react-app/jest"
- ]
- },
- "browserslist": {
- "production": [
- ">0.2%",
- "not dead",
- "not op_mini all"
- ],
- "development": [
- "last 1 chrome version",
- "last 1 firefox version",
- "last 1 safari version"
- ],
- "proxy": "http://localhost:5000"
- }
- }
使用方法一遇到了bug,去stackoverflow解决一下,能解决,但是有更好的方法,方法一不在继续了.node.js - Invalid options object. Dev Server has been initialized using an options object that does not match the API schema - Stack Overflow
- 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:199},
- {id:'002',name:'马自达',price:109},
- {id:'003',name:'捷达',price:120},
- ]
- response.send(cars)
- })
-
- app.listen(5001,(err)=>{
- if(!err) console.log('服务器2启动成功了,请求汽车信息地址为:http://localhost:5001/cars');
- })
访问端口:
在src下创建配置文件:src/setupProxy.js
这个适合低版本
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(
proxy('/api1', { //api1是需要转发的请求(所有带有/api1前缀的请求都会转发给5000)
target: 'http://localhost:5000', //配置转发目标地址(能返回数据的服务器地址)
changeOrigin: true, //控制服务器接收到的请求头中host字段的值
/*
changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000
changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:3000
changeOrigin默认值为false,但我们一般将changeOrigin值设为true
*/
pathRewrite: {'^/api1': ''} //去除请求前缀,保证交给后台服务器的是正常请求地址(必须配置)
}),
proxy('/api2', {
target: 'http://localhost:5001',
changeOrigin: true,
pathRewrite: {'^/api2': ''}
})
)
}
我使用的node是:v18.18.0
//react 18版本写法 const {createProxyMiddleware} = require('http-proxy-middleware') module.exports = function (app) { app.use( createProxyMiddleware('/api1', { target: 'http://localhost:5000', changeOrigin: true, pathRewrite: {'^/api1': ''} }), createProxyMiddleware('/api2', { target: 'http://localhost:5001', changeOrigin: true, pathRewrite: {'^/api2': ''} }), ) }
- import React, {Component} from 'react';
- import axios from "axios";
-
- 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 (
-
-
-
-
- );
- }
- }
-
- export default App;
优点:可以配置多个代理,可以灵活的控制请求是否走代理。
缺点:配置繁琐,前端请求资源时必须加前缀。
App.js
- import React, {Component} from 'react';
- import Search from './components/Search/Search'
- import List from "./components/List/List";
-
- class App extends Component {
- render() {
- return (
-
- "container">
-
-
-
-
- );
- }
- }
-
- export default App;
index.js
- import React from 'react';
- import ReactDOM from 'react-dom/client';
- import App from './App';
-
- const root = ReactDOM.createRoot(document.getElementById('root'));
- root.render(
);
List.jsx
- import React, {Component} from 'react';
- import './List.css'
-
- class List extends Component {
- render() {
- return (
-
- "row">
- "card">
- "head_protrait" src="https://avatars.githubusercontent.com/u/6412038?v=3" style={{width:'100px'}}/>
-
-
"card-text">reactjs
-
-
-
- );
- }
- }
-
- export default List;
List.css
- .album {
- min-height: 50rem; /* Can be removed; just added for demo purposes */
- padding-top: 3rem;
- padding-bottom: 3rem;
- background-color: #f7f7f7;
- }
-
- .card {
- float: left;
- width: 33.333%;
- padding: .75rem;
- margin-bottom: 2rem;
- border: 1px solid #efefef;
- text-align: center;
- }
-
- .card > img {
- margin-bottom: .75rem;
- border-radius: 100px;
- }
-
- .card-text {
- font-size: 85%;
- }
Search.jsx
- import React, {Component} from 'react';
-
- class Search extends Component {
- render() {
- return (
-
-
"jumbotron"> -
"jumbotron-heading">Search Github Users
-
- type="text" placeholder="enter the name you search"/>
-
-
-
-
- );
- }
- }
-
- export default Search;
运行结果:
demo.html
- "en">
- <head>
- "UTF-8">
-
Title