简体   繁体   English

如何使用自定义网络服务器做出反应?

[英]How can I use react with a custom webserver?

I want to use React for a project I am working on, but I also want to use an API.我想将 React 用于我正在处理的项目,但我也想使用 API。 How can I do it?我该怎么做?

I have tried to Google this and ask different people, but I have not got a response yet, so I thought I would ask here.我试过谷歌这个并询问不同的人,但我还没有得到回应,所以我想我会在这里问。 I want to use express and maybe not use create-react-app (as it takes up a lot of storage).我想使用express而可能不使用create-react-app (因为它占用大量存储空间)。

Working on a custom server doesen't preclude the use of an API.在自定义服务器上工作并不排除使用 API。

If you want fetch the API from the express server and inject it directly on react frontend you need to enable server side rendering (useful post ) and pass the data collected as a props from the server (check this example ).如果您想从 express 服务器获取 API 并将其直接注入到 React 前端,您需要启用服务器端渲染(有用的帖子)并将从服务器收集的数据作为道具传递(请查看此示例)。

Rather then you can build your react project (using even create-react-app) and build an express server who return the index.html on call.相反,您可以构建您的反应项目(甚至使用 create-react-app)并构建一个快速服务器,该服务器在调用时返回 index.html。

Personally I prefer the first one solution.我个人更喜欢第一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM