Is there a way to chain middleware on "ordinary" firebase functions like in express?
"ordinary" function
addNote = https.onRequest((req, res, next) => {
addNote(req, res,next);
});
app.post("addNote", isAuthenticated, validate, (req, res, next) => {
addNote(req, res, next);
}
);
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);
Now the /hello function is served by Cloud Functions, and has the cors and cookie-parser middleware applied to it.
Code segment taken from this sample.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With