如何将 nodeJS 项目托管到 Firebase?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45537195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:24:30  来源:igfitidea点击:

How to host nodeJS project to firebase?

node.jsfirebasefirebase-hosting

提问by jiancheng wu

I am using node, express and more other dependencies for the project. I wonder how to host this project on firebase. My project will have controller, view , and any other folders to make the project possible.It already has view engine like pug/handlebars.

我正在为项目使用 node、express 和更多其他依赖项。我想知道如何在 firebase 上托管这个项目。我的项目将有控制器、视图和任何其他文件夹来使项目成为可能。它已经有像 pug/handlebars 这样的视图引擎。

Tutorials online only show how to host firebase with single index.html in public folder. How am I suppose to host my project with all other folders? I know how to use firebase in nodeJS, but how to host the project on firebase? How firebase will access the server file(either app/index.js)? Where should I put all these folders?

在线教程仅展示如何在公共文件夹中使用单个 index.html 托管 firebase。我该如何与所有其他文件夹一起托管我的项目?我知道如何在 nodeJS 中使用 firebase,但如何在 firebase 上托管项目?firebase 将如何访问服务器文件(app/index.js)?我应该把所有这些文件夹放在哪里?

Hopefully I am not asking to much. If my question isn't clear, please let me know so that I can make clarification.

希望我没有要求太多。如果我的问题不清楚,请告诉我,以便我进行澄清。

回答by Frank van Puffelen

You're in luck. Firebase just this week released a video that walks step-by-step through setting up an Node.js/Express app on Firebase Hosting.

你很幸运。Firebase 本周刚刚发布了一个视频,视频逐步介绍了如何在 Firebase Hosting 上设置 Node.js/Express 应用程序

This has only been possible since the integration of Cloud Functions with Firebase Hosting, which was released at I/O 2017. Most tutorials likely are from before that time, when there was no way to run server-side code on Firebase Hosting.

自从 Cloud Functions 与 Firebase Hosting 集成以来,这才成为可能,该集成于 2017 年 I/O 上发布。大多数教程可能是在那之前,当时无法在 Firebase Hosting 上运行服务器端代码。

回答by Sunny Sultan

You can do this by firebase cloud function. Deployment auth is handled by the Firebase CLI, so getting a hello world up and running is literally:

您可以通过 firebase 云功能来做到这一点。部署身份验证由 Firebase CLI 处理,因此启动并运行 hello world 就是:

firebase init functions
firebase deploy

A simple hello world function

一个简单的hello world函数

functions.https.onRequest((request, response) => {
    response.send(“Hello from Firebase!”);
});

Note: You can use express with firebase function.

注意:您可以将 express 与 firebase 功能一起使用。

For more follow documentation link: https://firebase.google.com/docs/functions

有关更多信息,请访问文档链接:https: //firebase.google.com/docs/functions