声明单独的Firebase Cloud Functions并仍使用Express.js [英] Declare separate Firebase Cloud Functions and still use Express.js

查看:123
本文介绍了声明单独的Firebase Cloud Functions并仍使用Express.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多Express for Firebase Cloud功能的示例.

在我发现的每个示例中,代码都将Express应用程序公开为单个Cloud Function:

In every example of I have found the code exposes the Express app as a single Cloud Function:

exports.app = functions.https.onRequest(app);

对于一个人的Firebase项目功能,这意味着他们将看到一个名为"app"的条目,并且所有Express.js HTTP侦听器的所有日志都将移至Firebase中的一个位置.这也意味着,无论Express.js应用程序有多大,Firebase都会在该应用程序的生产环境中部署单个功能.

For one's Firebase Project Functions that means they will see one entry called "app" and all logs for all Express.js HTTP listeners will go to one place in Firebase. This also means, no matter how large one's Express.js app is, Firebase will deploy a single function in production for the app.

或者,当使用firebase-functions.https.onRequest时,对于每种导出,例如在Typescript中,您将获得单独的功能:

Alternatively, when using firebase-functions.https.onRequest you get separate functions for each export, for example, in Typescript:

export const hello = functions.https.onRequest(async (req, res) => {
  res.status(200).send('hello world');
});

在Firebase控制台中,我有hello函数,并且在index.js中还有另一个函数:

And in Firebase Console, I have my hello function and also another function in my index.js:

这也意味着Firebase将为每个功能创建不同的节点/实例:helloemphemeralKey.

This also means Firebase will create different nodes/instances for each function: hello and emphemeralKey.

在Firebase控制台中,我将为每个功能分别记录日志.

And I will get separate logging for each function in the Firebase Console.

我想使用中间件来确保将有效的身份验证令牌传递给这样的终端云功能

I would like to use middleware to ensure that valid auth tokens are being passed to my endpoint Cloud Functions like this Firebase example but I would prefer not to use a single "app" single Cloud Function, I would prefer a dedicated function for function export in my index.js.

推荐答案

感谢道格·史蒂文森(Doug Stevenson)的回答和帮助.我想提供自己的答案.

Thanks to Doug Stevenson for his answer and help. I wanted to provide my own answer though.

所以我的问题的答案通常是:不,你不能.

So the answer to my question is, generally speaking: no you can't.

正如道格指出的那样,对于许多人的扩展需求而言,这不是问题. Firebase最多可以创建1000个函数实例以进行扩展.

As Doug was pointing out, this is not a problem for many people's scaling needs. Firebase will create up to 1,000 instances of your function to scale.

我想提供与道格稍有不同的答案,说明我将如何编写Express应用并为项目提供不同的Firebase Cloud功能:

I wanted to provide a slightly different answer then Doug's to how I would write an Express app and have different Firebase Cloud Functions for a project:

const payment = express()
const order = express()
payment.get('/route', ...)
order.get('/route', ...)
export const payment = functions.https.onRequest(payment)
export const order = functions.https.onRequest(order)

这里的优点是我可以开始表达REST或RPC路由,如:

The advantage here is that I can start to express REST or RPC routes like:

  • /付款/某些行为(RPC)
  • /订单(获取,放置,发布等)

另一个好处是,我可以为信用卡付款/处理之类的事情提供测试" API和实时" API:

Another benefit is that I can provide a "test" API and a "live" API for things like credit card payments/processing:

// [START Express LIVE App]


// [START get user]
app.get('/user', async (req, res) => {
  await handleGetUser(req, res, paymentServiceLive);
});
// [END get user]

// [START claim]
app.post('/claim', async (req, res) => {
  await handleClaim(req, res, claimEmailTo);
});
// [END claim]

// [START user]
app.post('/user', async (req, res) => {
  await handleUserPost(req, res, paymentServiceLive);
});
// [END user]

// [START ephemeralKey]
app.post('/ephemeralKey', async (req, res) => {
  await handleEphemeralKey(req, res, paymentServiceLive);
});
// [END ephemeralKey]


// [START charge]
app.post('/charge', async (req, res) => {
  await handleCharge(req, res, paymentServiceLive);
});
// [END charge]

// [START purchase]
app.post('/purchase', async (req, res) => {
  await handlePurchase(req, res, paymentServiceLive);
});
// [END purchase]

//Expose Express API as a single Cloud Function:
exports.app = functions.https.onRequest(app);

// [END Express LIVE App]



// [START Express TEST App]

// [START get user]
appTest.get('/user', async (req, res) => {
  console.log('appTest /user get', req);
  await handleGetUser(req, res, paymentServiceTest);
});
// [END get user]

// [START claim]
appTest.post('/claim', async (req, res) => {
  await handleClaim(req, res, claimEmailToTest, true);
});
// [END claim]


// [START user]
appTest.post('/user', async (req, res) => {
  console.log('appTest /user post', req);
  await handleUserPost(req, res, paymentServiceTest);
});
// [END user]

// [START ephemeralKey]
appTest.post('/ephemeralKey', async (req, res) => {
  await handleEphemeralKey(req, res, paymentServiceTest)
});
// [END ephemeralKey]


// [START charge]
appTest.post('/charge', async (req, res) => {
  await handleCharge(req, res, stripeTest);
});
// [END charge]

// [START purchase]
appTest.post('/purchase', async (req, res) => {
  await handlePurchase(req, res, paymentServiceTest);
});
// [END purchase]

//Expose Express API as a single Cloud Function:np
exports.apptest = functions.https.onRequest(appTest);

// [END Express TEST App]

这使我拥有一个开发环境和一个实时环境.在我的应用程序配置文件中,我只有一个不同的API网址:

This allows me to have a development environment and a live environment. in my app config files I just have a different API url:

/us-central1/apptest

/us-central1/app

这篇关于声明单独的Firebase Cloud Functions并仍使用Express.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆