简体   繁体   English

如何在 firebase 上正确部署带有 NodeJS 后端的 ReactJS 前端

[英]How to properly deploy a ReactJS frontend with a NodeJS backend on firebase

To learn ReactJS and Firebase, I had create a web app with these technologies.为了学习 ReactJS 和 Firebase,我使用这些技术创建了一个 web 应用程序。

But I didn't publish this app on the web because, since react is a frontend library, we can see the code with my firebase infos.但是我没有在 web 上发布这个应用程序,因为由于 react 是一个前端库,我们可以在我的 firebase 信息中看到代码。

Now I have learned NodeJS and I want to implement that on my app for the backend and publish on firebase but I don't know how publish an app with both React and Nodejs on firebase.现在我已经学习了 NodeJS,我想在我的后端应用程序上实现它并在 firebase 上发布,但我不知道如何在 firebase 上发布一个同时使用 ReactNodejs 的应用程序。

The only idea I have is to run the npm build command, put the content of the build folder in a view folder of my Nodejs project, and redirect the home page to my React index like that:我唯一的想法是运行npm build命令,将build文件夹的内容放在我的 Nodejs 项目的view文件夹中,然后将主页重定向到我的 React 索引,如下所示:

app.get('/', (req, res) => {
  res.render('view/index');
});

and publish on firebase like a nodeJS project.并像 nodeJS 项目一样在 firebase 上发布。

I think this is a good solution but I want to be sure and know if a better solution exist because I want to use the best practices, and move the build content after each frontend update... (and I don't want to restart all my backend if I don't know how publish after)我认为这是一个很好的解决方案,但我想确定并知道是否存在更好的解决方案,因为我想使用最佳实践,并在每次前端更新后移动构建内容......(而且我不想重新启动如果我不知道之后如何发布,我所有的后端)

It's not totally clear, but it sounds like you're asking how to use a serverless Firebase function as a back end for your react project.这并不完全清楚,但听起来你在问如何使用无服务器 Firebase function 作为你的反应项目的后端。

To do that, you simply create an HTTPS function that serves as an endpoint for your front end to send data to, which is then processed and sent back, or logged, or interacts with a database, etc.为此,您只需创建一个 HTTPS function 作为前端发送数据的端点,然后处理并发回,或记录,或与数据库交互等。

Setting up an HTTPS function is easy: you run firebase init in a directory, choose 'functions' when asked what to initialize, and and the CLI will generate The necessary files for your project.设置 HTTPS function 很容易:您在目录中运行firebase init ,在询问将生成什么文件初始化项目时选择“函数”,然后

In your index.js file, you write the node.js code for your function, and then run firebase deploy from that same directory.在 index.js 文件中,为 function 编写 node.js 代码,然后从同一目录运行firebase deploy

Then your endpoint will be live, and it can consume any requests you send it to it.然后您的端点将处于活动状态,它可以使用您发送给它的任何请求。 For more, see the official documentation on Firebase functions.有关更多信息,请参阅 Firebase 函数的官方文档。

. . References:参考:

https://firebase.google.com/docs/cli#initialize_a_firebase_project https://firebase.google.com/docs/cli#initialize_a_firebase_project

https://firebase.google.com/docs/functions/get-started https://firebase.google.com/docs/functions/get-started

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

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