为什么 Firebase 不是 运行 我的函数文件夹(对于节点应用程序)?

Why is Firebase not running my functions folder (for a node app)?

我正在尝试在 firebase 上托管节点应用程序

问题是当我 运行 一个非常基本的时间戳函数时它总是超时。

这是 firebase.json 文件:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "rewrites": [{
      "source": "/timestamp",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

这是函数文件夹中的 package.json 文件:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "main": "index.js",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "jquery": "^3.5.1",
    "node": "^14.8.0",
    "nodemailer": "^6.4.11",
    "nodemailer-mailgun-transport": "^2.0.0",
    "nodemon": "^2.0.4"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

这里是函数文件夹中的index.js:

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

const app = express();
app.get('/timestamp', (request, response)=>{
    response.send(`${Date.now()}`);
});

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest(app);

这是我在本地 运行 firebase 时的终端日志:

Andrews-iMac:functions test$ firebase serve --only functions,hosting
⚠  Your requested "node" version "10" doesn't match your global version "12"
i  functions: Watching "/Users/test/Desktop/Git/Deep Technology/deep-technology/functions" for Cloud Functions...
i  hosting: Serving hosting files from: public
✔  hosting: Local server: http://localhost:5000
[hosting] Rewriting /timestamp to http://localhost:5001/deep-technology/us-central1/app for local Function app
✔  functions[helloWorld]: http function initialized (http://localhost:5001/deep-technology/us-central1/helloWorld).
i  hosting: 127.0.0.1 - - [18/Aug/2020:17:32:31 +0000] "GET /timestamp HTTP/1.1" 504 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"
[hosting] Rewriting /timestamp to http://localhost:5001/deep-technology/us-central1/app for local Function app
i  hosting: 127.0.0.1 - - [18/Aug/2020:17:33:50 +0000] "GET /timestamp HTTP/1.1" - - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"

这里是我运行在本地主机上的截图(你也可以看到文件结构):

我想不通,但是,你的问题很完美,我可以很容易地在我这边复制它。试了几次,我找到了,但这是一个很小的错误,但是这样的错误是最难发现的。

firebase.jsonhosting 中提供的代码函数与从 index.js 中导出的函数不同。所以有2个解决方案:

firebase.json 托管中应该有 "function": "helloWorld"

index.js中应该有:exports.app = functions.https.onRequest(app);

我这边都很好用!

就我而言,我转到了 firebase.json 文件,我最初将其作为:

{
    "database": {
        "rules": "database.rules.json"
    }
}

我改成了:

{
  "hosting": {
    "public": "public",
    "rewrites": [{
      "source": "**",
      "function": "app"
    }]
  }
}