有没有办法像 Express 一样在“普通”firebase 函数上链接中间件?
"ordinary“ 功能
addNote = https.onRequest((req, res, next) => {
addNote(req, res,next);
});
using express I chained isAuthenticated and validate middleware
app.post("addNote", isAuthenticated, validate, (req, res, next) => {
addNote(req, res, next);
}
);
自动应用 Express 中间件的唯一方法是为端点(或端点集合)创建 Express 应用程序并向其应用中间件。然后,该 Express 应用程序可以使用 Cloud Functions for Firebase 处理 HTTP 端点。例如:
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 中间件。
代码段取自这个样本 https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)