google 定义简单函数时出现云函数错误
google cloud function error when defining simple function
你好,我正在尝试使用 firebase 托管部署一个简单的函数,当我这样做时,我的 firebase 日志中出现以下错误:
TypeError: Cannot set property 'content-type' of undefined
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:365:22)
at ServerResponse.header (/user_code/node_modules/express/lib/response.js:767:10)
at ServerResponse.contentType (/user_code/node_modules/express/lib/response.js:595:15)
at ServerResponse.send (/user_code/node_modules/express/lib/response.js:145:14)
at app.get (/user_code/lib/index.js:10:22)
at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
at next (/user_code/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/user_code/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
at /user_code/node_modules/express/lib/router/index.js:281:22
鉴于我刚刚开始使用 firebase 托管,我不知道是什么原因造成的;这是我的文件:
src > index.ts:
import * as functions from 'firebase-functions';
import * as express from 'express';
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
const app = express();
export const getTime = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
firebase.json:
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites":[{
"source": "/timestamp",
"function": "getTime"
}]
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}
package.json:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"express": "^4.16.4",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
如您所见 here,您的 index.ts 应该是:
import * as functions from 'firebase-functions';
import * as express from 'express';
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
const app = express();
app.get("*", (request, response) => {
response.send("Hello from Firebase!");
});
export const getTime = functions.https.onRequest(app);
你好,我正在尝试使用 firebase 托管部署一个简单的函数,当我这样做时,我的 firebase 日志中出现以下错误:
TypeError: Cannot set property 'content-type' of undefined
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:365:22)
at ServerResponse.header (/user_code/node_modules/express/lib/response.js:767:10)
at ServerResponse.contentType (/user_code/node_modules/express/lib/response.js:595:15)
at ServerResponse.send (/user_code/node_modules/express/lib/response.js:145:14)
at app.get (/user_code/lib/index.js:10:22)
at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
at next (/user_code/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/user_code/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/user_code/node_modules/express/lib/router/layer.js:95:5)
at /user_code/node_modules/express/lib/router/index.js:281:22
鉴于我刚刚开始使用 firebase 托管,我不知道是什么原因造成的;这是我的文件:
src > index.ts:
import * as functions from 'firebase-functions';
import * as express from 'express';
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
const app = express();
export const getTime = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
firebase.json:
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites":[{
"source": "/timestamp",
"function": "getTime"
}]
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}
package.json:
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"express": "^4.16.4",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
},
"devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
如您所见 here,您的 index.ts 应该是:
import * as functions from 'firebase-functions';
import * as express from 'express';
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
const app = express();
app.get("*", (request, response) => {
response.send("Hello from Firebase!");
});
export const getTime = functions.https.onRequest(app);