admin管理员组

文章数量:1415484

I'm having a problem using Express middleware with Firebase Functions. In this example a function is hooked to the app() instance like so:

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

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

How do I go about having multiple functions using express middleware?

This is one of the ways I've tried solving it but calling the endpoints returns a 404:

app.get('/authorizedHello', (request, response) => {
    response.send(`Hello ${request.user.name}`);
})

app.get('/authorizedBye', (request, response) => {
    response.send(`Bye ${request.user.name}`);
})

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

I'm sure I'm just doing it wrong. Could you point me in the right direction?

I'm having a problem using Express middleware with Firebase Functions. In this example a function is hooked to the app() instance like so:

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

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

How do I go about having multiple functions using express middleware?

This is one of the ways I've tried solving it but calling the endpoints returns a 404:

app.get('/authorizedHello', (request, response) => {
    response.send(`Hello ${request.user.name}`);
})

app.get('/authorizedBye', (request, response) => {
    response.send(`Bye ${request.user.name}`);
})

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

I'm sure I'm just doing it wrong. Could you point me in the right direction?

Share Improve this question asked Mar 23, 2017 at 17:40 Andrey PokrovskiyAndrey Pokrovskiy 2,9362 gold badges14 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Consider that the base path of the cloud function uses your export name, so in this case your valid URLs will be:

https://us-central1-<YOURAPP>.cloudfunctions/authorizedHello/authorizedHello
https://us-central1-<YOURAPP>.cloudfunctions/authorizedHello/authorizedBye
https://us-central1-<YOURAPP>.cloudfunctions/authorizedBye/authorizedHello
https://us-central1-<YOURAPP>.cloudfunctions/authorizedBye/authorizedBye

That's why the example uses get('*', ...). You should see your URLs displayed after the deploy mand executes.

You have four URLs because your exporting the same Express app twice.

thank you for your great question. Your question is give me a clue for using template engine .Maybe i'm out of topic of your question . BUT , i just wanna share if anyone want to use template engine like Pug . Look my sample codes here.

const functions = require('firebase-functions');
const express = require("express");
const app = express();




app.set("views",__dirname+"/tmp");
app.set("view engine","pug");
app.engine('pug', require('pug').__express);
app.get("/",p3p(p3p.remended),function(req,res){
   res.render("index");
});
app.get("/login",p3p(p3p.remended),function(req,res){
   res.render("login");
});
exports.main = functions.https.onRequest(app);

And then you can access that to link like this . https://us-central1-[YOURAPP].cloudfunctions/main/ https://us-central1-[YOURAPP].cloudfunctions/main/login

Sorry for bothering your question. But i had to find my question on google like using "How to use template engine to google cloud functions" i never got the right answer .

I'm just happy because of your sample codes in your question . That's inpiring me to do some improvements. Thank you . Sorry for my bad english :)

本文标签: javascriptUsing Express middleware in Firebase Functions HTTPS requestsStack Overflow