create-react-app之后端集成

1.在开发中代理API请求

人们通常在与后端实现相同的主机和端口上为前端React应用提供服务。

例如,在部署应用程序后,生产设置可能如下所示:

/             - static server returns index.html with React app
/todos        - static server returns index.html with React app
/api/todos    - server handles any /api/* requests using the backend implementation

这样的设置不是必需的。但是,如果您确实有这样的设置,则可以方便地编写请求,fetch('/api/todos')而不必担心在开发过程中将其重定向到另一个主机或端口。

要告诉开发服务器将任何未知请求代理到开发中的API服务器,请在package.json中添加一个proxy字段,例如:

  "proxy": "http://localhost:4000",

这样,在您fetch('/api/todos')进行开发时,开发服务器将识别出它不是静态资产,并将您的

猜你喜欢

转载自blog.csdn.net/qq_27868061/article/details/112411984