为什么Firebase无法运行我的functions文件夹对于节点应用程序?

问题描述

我正在尝试在Firebase上托管节点应用

问题在于,当我运行真正的基本时间戳功能时,它会一直超时。

这是firebase.json文件

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

这是functions文件夹中的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
}

这是funtions文件夹中的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"

这是我运行本地主机的屏幕截图(您也可以看到文件结构):

enter image description here

解决方法

我无法弄清楚,但是,您的问题很完美,我能够轻松地在我这一边重复。经过几次尝试,我发现了它,但这是一个很小的错误,但是最难以发现这种错误。

hostingfirebase.json中提供的代码中的功能与从index.js中导出的功能不同。因此,有两种解决方案:

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"
    }]
  }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...