2017-03-23 15 views
1

我在使用Express中间件和Firebase函数时遇到问题。在这种example功能被钩到应用程序()实例,像这样:在Firebase函数中使用Express中间件HTTPS请求

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

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

如何去使用快递中间件具有多种功能?

这是我试图解决它,但调用终端的一种方式返回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); 

我敢肯定,我只是做是错误的。你能指出我正确的方向吗?

回答

3

认为云计算功能的基本路径使用您的出口名称,所以在这种情况下,您的有效的网址是:

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

这就是为什么示例使用get('*', ...)。执行deploy命令后,您应该看到显示的URL。

您有四个URL,因为您导出两次相同的Express应用程序。

+0

谢谢指点/ authorizedHello/authorizedHello工作!后续问题。有没有办法避免在API调用中有两次函数名? –

+1

我建议只做一些像'exports.api = functions.https.onRequest(app);'一次。或'exports.authorized'然后'get('/ hello',...)''get('/ bye',...)' –

+0

或者你可以制作两个不同的快递应用程序并允许'get('*' ,...)'。在我的回答中还澄清说,您目前有四个有效的网址。 –

1

谢谢你的好问题。你的问题是给我一个使用模板引擎的线索。可能我没有你的问题的主题。但是,我只是想分享,如果有人想使用模板引擎像帕格。看看我的示例代码。

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.recommended),function(req,res){ 
 
    res.render("index"); 
 
}); 
 
app.get("/login",p3p(p3p.recommended),function(req,res){ 
 
    res.render("login"); 
 
}); 
 
exports.main = functions.https.onRequest(app);

然后你就可以访问到这样的链接。 https://us-central1-[YOURAPP].cloudfunctions.net/main/ https://us-central1-[YOURAPP].cloudfunctions.net/main/login

对不起,打扰你的问题。但我不得不在谷歌上找到我的问题,就像使用“如何使用模板引擎来谷歌云功能”我从来没有得到正确的答案。

我只是很高兴,因为你的问题中的示例代码。这帮助我做了一些改进。谢谢 。 对不起,我的英文不好:)