简体   繁体   English

在 Firebase Cloud Functions 中使用 express 和 consign 进行路由

[英]Routing with express and consign in Firebase Cloud Functions

I want to implement routing in my Express app which is to be exposed through a cloud function.我想在我的 Express 应用程序中实现路由,该应用程序将通过云功能公开。

Here is my functions/index.js file:这是我的函数/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);

This is my./routes/index.js file这是我的./routes/index.js 文件

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

So I guess this code is enough to host a cloud function and when I call this hosting url https://us-central1-appname-79516.cloudfunctions.net/api (url changed for reason of privacy) it should return response as {"status":"success":}所以我想这段代码足以托管一个云函数,当我调用这个托管 url https://us-central1-appname-79516.cloudfunctions.net/api (出于隐私原因更改 url)时,它应该返回响应为 { “状态”:“成功”:}

Instead when I call the above url it displays the error "Error: could not handle the request"相反,当我调用上面的 url 时,它显示错误“错误:无法处理请求”

Help me how to use express and consign module in a cloud function帮助我如何在云功能中使用快递和托运模块

This is a bit late but still relevant I guess.这有点晚了,但我想仍然很重要。 So, this is what I've done:所以,这就是我所做的:

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

CWD sets the base directory, as stated on the docs: 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 https://github.com/jarradseers/consign

In my case, src is the folder containing my firebase functions code (functions/src).在我的例子中, src是包含我的 firebase 函数代码的文件夹 (functions/src)。

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM