使用快速路由并在Firebase云功能中委托

时间:2017-07-18 09:11:06

标签: node.js express firebase google-cloud-functions

我想在我的Express应用程序中实现路由,该应用程序将通过云功能公开。

这是我的函数/ index.js文件:

const functions = require('firebase-functions');
const express = require('express');
const consign = require('consign');

const app = express();

  consign()
    .include("./routes")
    .into(app);

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

这是我的./routes/index.js文件

module.exports = app => {
    app.get('/', (req,res)=>{
    res.json({status:"success"});
})
}

所以我猜这段代码足以托管云端功能,当我调用此托管网址https://us-central1-appname-79516.cloudfunctions.net/api(因私密原因而更改网址)时,它应返回响应为{“status”:“success”:}

相反,当我调用上面的url时,它会显示错误“错误:无法处理请求”

帮助我如何在云功能中使用快递和委托模块

1 个答案:

答案 0 :(得分:1)

这有点晚了,但我想仍然很有意义。所以,这就是我所做的:

consign({
  cwd: 'src'
})
.include("routes")
.into(app)

CWD设置基本目录,如文档所述:

Consign will simply use a relative path from your current working directory, however, sometimes you don't want heavily nested files included in the object chain, so you can set the cwd

https://github.com/jarradseers/consign

就我而言,src是包含Firebase函数代码(functions / src)的文件夹。

希望有帮助!