繁体   English   中英

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

[英]Routing with express and consign in Firebase 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"});
})
}

所以我想这段代码足以托管一个云函数,当我调用这个托管 url https://us-central1-appname-79516.cloudfunctions.net/api (出于隐私原因更改 url)时,它应该返回响应为 { “状态”:“成功”:}

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

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

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

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)。

希望能帮助到你!

暂无
暂无

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

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