目前我正在学习express和Firebase。我可以将 Angular 或 React 页面(前端)部署到 Firebase。
现在,我正在尝试使用 Express 创建后端,然后我意识到,我不能简单地将其作为另一个项目部署到 Firebase,因为 Firebase 托管将寻找 index.html 作为入口点(*如果我,请纠正我)错了)。我已经尝试过 Firebase 功能,但仍然不行。
我可以将 javascript (expressjs) 文件设置为 Firebase 作为入口点吗?
thanks.
您可以将 Express 应用程序部署到Firebase 的云功能 https://firebase.google.com/docs/functions/ as an HTTP触发器 https://firebase.google.com/docs/functions/http-events。有一个sample https://github.com/firebase/functions-samples/tree/master/authenticated-json-api这说明了这一点。 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)
现在您可以访问该路径下的路线https://您分配的主机名/api/whatever https://your-assigned-hostname/api/whatever
EDIT
您还需要配置您的firebase.json如果您想将每个请求重定向到 Express 应用程序,请使用此文件。
在里面添加这一行hosting目的:"rewrites" : [{"source" : "**", "function" : "api"}]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)