使用 Raspberry Pi 4 来托管 React Web 应用程序

Using a Raspberry Pi 4 to host react web app

我对如何在 Raspberry Pi 上设置网络应用程序的流程有一些广泛的疑问。

我在我的本地 PC 上构建了一个 React 应用程序,它触发了一个 node/express API 我放在一起然后在我的 MYSQL 上执行基本的 CRUD数据库。我想使用 Raspberry Pi 从互联网上的任何地方访问整个东西。我已经设法将 API 和数据库都转移到 raspberry pi 并且它工作正常。我不认为 React 前端会成为问题,但我有这些问题挥之不去:

我的第一个建议是 Docker, but I understand if you just want a server. I think the best way to go in this case would be to use something like Next.js to combine the API app and the React app and get server-side rendering as a bonus (so you don't even necessarily need API; you can call the server functions in your app using getServerSideProps). You could then use Nginx as a reverse proxy to expose the Next.js server. See here to learn how to set up Nginx on Raspberry Pi and here 学习如何为 Next.js 设置它。

如果您不想使用 Next.js(已经构建了您的应用程序,或其他原因),请注意 create-react-app 或您正在使用的任何其他内容可能会生成静态文件服务,在这种情况下,您只需设置 Nginx,然后将构建文件夹复制到 Nginx 静态文件夹(参见 here). If you're only exposing the front-end then you don't need to do anything else to expose the express server, but if you do want to expose the express server, you can read more about that here(使用 express 而不是 HTTP)。

如果您完全愿意使用 Raspberry Pi,那很好。如果您决定使用 Next.js,甚至只是 React,您也可以在 Vercel or Netlify. You could use their serverless functions as API endpoints for your application (set up automatically with Next.js, otherwise read here for Vercel and here 上为 Netlify 托管您的网站。