使用Firebase云端功能实现REST界面 [英] Implementing a REST interface with Firebase Cloud Functions

查看:223
本文介绍了使用Firebase云端功能实现REST界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为云功能提供的HTTP API将所有HTTP方法混合成一个API调用:

  functions.https.onRequest (req,res)=> {...}); 

这需要一个开关来分隔GET,POST,PUT和DELETE。是否有类似于Express API的更好的方法:

$ app $($)
app.post()
app.put()
app.delete()

更清晰地实现基于资源的端点。 Firebase的云端函数允许您提供 Express.js 应用程序来实现HTTP API(或任何其他你想要做的与Express.js应用程序)。



你可以在此示例代码

需要注意的重要一点是,您可以创建一个快速应用程序,使用您需要的端点进行配置,然后将其交给云端函数:

  const express = require('exp RESS’); 
const app = express();
app.get('/ hello',(req,res)=> {
res.send(`Hello $ {req.user.name}`);
});
exports.app = functions.https.onRequest(app);


The HTTP API provided for the cloud functions mixes all HTTP methods into one API call:

functions.https.onRequest((req, res) => { ... });

This requires a switch to separate out the GETs, POSTs, PUTs and DELETEs. Is there a better way similar to the Express API:

app.get()
app.post()
app.put()
app.delete()

This will allow us to implement resource based endpoints more cleanly.

解决方案

Cloud Functions for Firebase allows you to provide an Express.js app to implement HTTP APIs (or whatever else you want to do with an Express.js app).

You can see an example of this in this sample code.

The important thing to note is that you can create an express app, configure it with the endpoints you want, and hand it to Cloud Functions:

const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
  res.send(`Hello ${req.user.name}`);
});
exports.app = functions.https.onRequest(app);

这篇关于使用Firebase云端功能实现REST界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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