如何拆分firebase功能?

时间:2017-11-10 08:03:31

标签: firebase express google-cloud-functions

我正在使用firebase函数,很难将其作为index.js

index.js文件如:

  

index.js

我希望在index.js中使用express.js

成为apitest.js 子域路由
  

index.js
  apis - > test.js

index.js

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

const app = express();
...
const funcTestApi = require('./apis/apitest');

exports.apitest = functions.https.onRequest(app => {
    funcTestApi.router(app);
});

apitest.js

exports.router = function(app) {
    app.get('/foo', (req, res) => {
        res.status(200).json('hello');
    });

    app.get('/bar', (req, res) => {
        res.status(200).json('world');
    });

    ... // other url route 
}

日志:

http http://localhost:5000/project-name/us-central1/apitest/foo
http http://localhost:5000/project-name/us-central1/apitest/bar
...

info: Execution took 60000 ms, finished with status: 'timeout'

它不起作用。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。写回程代码,然后是它的工作

<强> apitest.js

exports.router = function(app) {
    app.get('/foo', (req, res) => {
        res.status(200).json('hello');
    });

    app.get('/bar', (req, res) => {
        res.status(200).json('world');
    });

    ... // other url route 

    return app; // <-- here
}