问题描述
我正在尝试在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"
这是我运行本地主机的屏幕截图(您也可以看到文件结构):
解决方法
我无法弄清楚,但是,您的问题很完美,我能够轻松地在我这一边重复。经过几次尝试,我发现了它,但这是一个很小的错误,但是最难以发现这种错误。
hosting
中firebase.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"
}]
}
}