如何将Express部署到Firebase? [英] How to deploy express to Firebase?
问题描述
目前,我正在学习Express和Firebase.我可以向Firebase部署角度页面或React页面(前端).
Currently i'm learning express and Firebase. I can deploy an angular or react page (front-end) to Firebase.
现在,我正在尝试使用express创建后端,然后我意识到,我不能简单地将其作为另一个项目部署到Firebase,因为Firebase托管将寻找index.html作为入口点(*请正确我,如果我错了).我已经尝试过Firebase函数,但仍然不能.
And now, i'm trying to create the back-end using express, then i realize, i cant just simply deploying it as another project to Firebase since Firebase hosting will looking for index.html as entry point (*please correct me if i'm wrong). I've tried Firebase functions but still can't.
我可以将javascript(expressjs)文件设置为Firebase作为入口点吗?
Can i set a javascript (expressjs) file to Firebase as entry point?
谢谢.
推荐答案
您可以将Express应用程序部署到 Firebase Cloud Functions 作为 HTTP触发器.有一个示例对此进行了说明.实现的基本形式在您的Cloud Functions index.js中如下所示:
You can deploy an Express app to Cloud Functions for Firebase as an HTTP trigger. There is a sample that illustrates this. The basic form of implementation looks like this in your Cloud Functions index.js:
const functions = require('firebase-functions')
const express = require('express')
const app = express()
// configure app here, add routes, whatever
exports.api = functions.https.onRequest(app)
现在,您可以在路径
编辑
如果要将每个请求重定向到Express应用,还需要配置 firebase.json 文件.
You also need to configure your firebase.json file if you want to redirect every request to the Express app.
将此行添加到 hosting 对象内:"rewrites" : [{"source" : "**", "function" : "api"}]
Add this line inside the hosting object: "rewrites" : [{"source" : "**", "function" : "api"}]
这篇关于如何将Express部署到Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!