Firebase功能链中间件 [英] firebase functions chain middleware

查看:65
本文介绍了Firebase功能链中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将中间件链接到Express中的普通" firebase函数上?

Is there a way to chain middleware on "ordinary" firebase functions like in express?

"普通"功能

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


使用快递,我链接了isAuthenticated并验证了中间件


using express I chained isAuthenticated and validate middleware

app.post("addNote", isAuthenticated, validate, (req, res, next) => {
  addNote(req, res, next);
 }
);

推荐答案

您可以自动应用Express中间件的唯一方法是为端点(或端点集合)创建Express应用,然后将中间件应用到该应用.然后,该快速应用可以使用Cloud Functions for Firebase处理HTTP端点.例如:

The only way you can automatically apply express middleware is to create an Express app for an endpoint (or collection of endpoints) and apply middleware to it. That express app can then handle HTTP endpoints with Cloud Functions for Firebase. For example:

const cookieParser = require('cookie-parser')();
const cors = require('cors')({origin: true});
const app = express();

app.use(cors);
app.use(cookieParser);

app.get('/hello', (req, res) => {
  res.send(`Hello ${req.user.name}`);
});

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

现在/hello函数由Cloud Functions提供,并已应用了cors和cookie-parser中间件.

Now the /hello function is served by Cloud Functions, and has the cors and cookie-parser middleware applied to it.

摘自此示例的代码段.

这篇关于Firebase功能链中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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