Google 云函数 :: 不能使用 'require' 语句
Google cloud functions :: cant use 'require' statements
问题
当我在 google 云函数中包含任何 'require' 语句时,我收到警告:"Function is active, but the last deploy failed"
解决方案 ?
我很确定我需要在 package.json 文件中包含依赖项,但我不知道要包含哪些依赖项,也不知道如何编写。
背景
我在 Java 中构建了一个 android 应用程序,我正在尝试集成条纹支付。 Stripe 要求我让服务器处理请求,但我正在尝试使用 google 云功能(因此我不必支付/管理服务器)。
我正在尝试遵循 this 示例,但它不起作用。作者没有包含 package.json 文件,我不确定要在其中放置哪些依赖项。我以前从未写过 java 脚本,我的背景是 python、c++、java。
我看过 this tutorial from google as well as the google docs on writing cloud functions。我也搜索了 S.O。并且找不到解决方案。问题可能是我不是 java 脚本开发人员。我正在尝试即插即用其他人的代码,以使我的 android (java) 应用程序的特定部分正常工作。
疑难解答
为了排除故障,我使用了 google 提供的 "helloWorld" 示例。 Hello World 函数可以自行查找。如果我在顶部添加这三个 require 语句中的任何一个,我会收到警告:"Function is active, but the last deploy failed"
代码
-- index.js
var app = require('express')();
var http = require('http').Server(app);
var stripe = require('stripe')(
"your_stripe_key"
);
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//initiate a one-off charge for a customer
exports.chargeCustomer = app.get(".../charge/:customerid/:token/:amount", function chargeCustomer (req,res){
stripe.charges.create({
customer: req.params.customerid,
source: req.params.token,
currency: 'usd',
amount:req.params.amount
},function(err, charge) {
if(err) {
return res.send(JSON.stringify(err));
}
res.send(JSON.stringify(charge));
});
});
-- package.json
{
"name": "sample-http",
"version": "0.0.1"
}
注释
如果您知道我做错了什么,请记住我从未写过 java脚本。我不认为我在问重复的问题,我已经搜索过,但答案可能在另一个问题中,我只是不理解它,因为我从未写过 javascript.
Here is an example with a package.json
, it's mentioned in this 文档。我将通过其内容的示例:
{
"nyc": {
"exclude": [
"build/src/apis",
"build/test"
]
},
"license": "Apache-2.0",
"dependencies": {
"google-auth-library": "^1.1.0",
"qs": "^6.5.1",
"string-template": "1.0.0",
"uuid": "^3.1.0"
},
...
}
也可以考虑阅读 npm.js
' documentation,因为 package.json
是通用的,不是特定于 Cloud Function 的。
我写了你在上面的问题中引用的 repo。问题是您没有正确格式化 package.json
文件。
部署函数时,您需要部署 index
文件和相应的 package.json
。您可以通过命令行进行部署,也可以仅使用 Cloud Functions 产品中的 Google 内联编辑器进行部署。
package.json
文件指定了您的代码 运行 所需的 依赖项 。基本上,您的索引文件需要知道 require
哪些第 3 方库的功能。您会注意到在我的示例代码中,下面包含一个 "dependencies"
的小节点,它将告诉 Google Cloud Functions 要与您的代码一起安装哪些包。
您引用的特定 index
文件正在通过 Stripe 创建费用。我在许多生产产品中使用此代码的变体,package.json
文件如下所示:
{
"name": "createCharge",
"version": "1.0.0",
"description": "create and charge in Stripe",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"stripe": "^4.4.0",
"body-parser": "~1.13.3",
"async": "^2.0.1",
"promise": "^7.1.1"
},
"engines": {
"node": "4.1.1"
},
"repository": {
"type": "git",
"url": "<path/to/your/repo.git>"
}
}
此外,请注意,上面 javascript 的 "repository"
块不是必需的,因此如果您没有此特定功能的托管存储库,请随意删除它。
希望对您有所帮助!
问题
当我在 google 云函数中包含任何 'require' 语句时,我收到警告:"Function is active, but the last deploy failed"
解决方案 ?
我很确定我需要在 package.json 文件中包含依赖项,但我不知道要包含哪些依赖项,也不知道如何编写。
背景
我在 Java 中构建了一个 android 应用程序,我正在尝试集成条纹支付。 Stripe 要求我让服务器处理请求,但我正在尝试使用 google 云功能(因此我不必支付/管理服务器)。
我正在尝试遵循 this 示例,但它不起作用。作者没有包含 package.json 文件,我不确定要在其中放置哪些依赖项。我以前从未写过 java 脚本,我的背景是 python、c++、java。
我看过 this tutorial from google as well as the google docs on writing cloud functions。我也搜索了 S.O。并且找不到解决方案。问题可能是我不是 java 脚本开发人员。我正在尝试即插即用其他人的代码,以使我的 android (java) 应用程序的特定部分正常工作。
疑难解答
为了排除故障,我使用了 google 提供的 "helloWorld" 示例。 Hello World 函数可以自行查找。如果我在顶部添加这三个 require 语句中的任何一个,我会收到警告:"Function is active, but the last deploy failed"
代码
-- index.js
var app = require('express')();
var http = require('http').Server(app);
var stripe = require('stripe')(
"your_stripe_key"
);
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//initiate a one-off charge for a customer
exports.chargeCustomer = app.get(".../charge/:customerid/:token/:amount", function chargeCustomer (req,res){
stripe.charges.create({
customer: req.params.customerid,
source: req.params.token,
currency: 'usd',
amount:req.params.amount
},function(err, charge) {
if(err) {
return res.send(JSON.stringify(err));
}
res.send(JSON.stringify(charge));
});
});
-- package.json
{
"name": "sample-http",
"version": "0.0.1"
}
注释
如果您知道我做错了什么,请记住我从未写过 java脚本。我不认为我在问重复的问题,我已经搜索过,但答案可能在另一个问题中,我只是不理解它,因为我从未写过 javascript.
Here is an example with a package.json
, it's mentioned in this 文档。我将通过其内容的示例:
{
"nyc": {
"exclude": [
"build/src/apis",
"build/test"
]
},
"license": "Apache-2.0",
"dependencies": {
"google-auth-library": "^1.1.0",
"qs": "^6.5.1",
"string-template": "1.0.0",
"uuid": "^3.1.0"
},
...
}
也可以考虑阅读 npm.js
' documentation,因为 package.json
是通用的,不是特定于 Cloud Function 的。
我写了你在上面的问题中引用的 repo。问题是您没有正确格式化 package.json
文件。
部署函数时,您需要部署 index
文件和相应的 package.json
。您可以通过命令行进行部署,也可以仅使用 Cloud Functions 产品中的 Google 内联编辑器进行部署。
package.json
文件指定了您的代码 运行 所需的 依赖项 。基本上,您的索引文件需要知道 require
哪些第 3 方库的功能。您会注意到在我的示例代码中,下面包含一个 "dependencies"
的小节点,它将告诉 Google Cloud Functions 要与您的代码一起安装哪些包。
您引用的特定 index
文件正在通过 Stripe 创建费用。我在许多生产产品中使用此代码的变体,package.json
文件如下所示:
{
"name": "createCharge",
"version": "1.0.0",
"description": "create and charge in Stripe",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"stripe": "^4.4.0",
"body-parser": "~1.13.3",
"async": "^2.0.1",
"promise": "^7.1.1"
},
"engines": {
"node": "4.1.1"
},
"repository": {
"type": "git",
"url": "<path/to/your/repo.git>"
}
}
此外,请注意,上面 javascript 的 "repository"
块不是必需的,因此如果您没有此特定功能的托管存储库,请随意删除它。
希望对您有所帮助!