使用Node.JS的iOS Firebase Flashlight / ElasticSearch Heroku安装程序

我正在使用Firebase后端在Swift中构build一个iOS应用程序。 我需要高级search选项为我的应用程序。 我被告知我应该通过这个github链接使用Flashlight / ElasticSearch https://github.com/firebase/flashlight

假设我的应用程序的名称是SneakerSearch和我的

Firebase Project_ID is- sneakersearch-az12 Firebase Web_API_Key is- abc123XYZ000... Firebase App_Url is- gs://sneakersearch-az12.appspot.com 

我需要一些build议与设置这个步骤,因为我是新的Heroku和Node.js。 我以前从来没有学过,但我已经安装了Heroku工具带,并完成“sudo gem heroku安装”。

我列出了Github的方向/步骤和我为每个方向所采取的步骤。 我需要指导帮助:1,4,5,6,9,10,12和15。

 1.Install and run ElasticSearch or add Bonsai service via Heroku 

我在Heroku做了一个帐户。

1.问题:我是否需要创build应用程序的名称,并在设置Heroku帐户后进行部署,还是应该等到步骤#9?

 2.git clone https://github.com/firebase/flashlight 

2.我把上面的东西克隆到我的iOS应用程序的项目文件夹中

 3.npm install 

3.提示我首先运行“npm init”,然后运行“npm install”

 4. edit config.js (see comments at the top, you must set FB_URL and FB_SERVICEACCOUNT at a minimum) 

问题:什么是“FB_SERVICEACCOUNT”? 我正在使用Firebase 3,但无法在控制台的服务帐户中find任何内容。 这是另一个Firebase帐户,我必须使用node.js为我的项目设置除了已经完成的Firebase swift项目吗?

4B.-问题:我在哪里“编辑config.js”并将我的FB_URL和FB_SERVICEACCOUNT设置为? 这应该在我的package.json文件?

 5.node app.js (run the app) 

5.-问题:在terminal中,我运行了“node app.js”并得到“throw err; ^”。 为什么我得到这个错误?

 6.curl -X POST http://localhost:9200/firebase 

6.问:这是为了什么?

 7.cd flashlight 

7.转换到手电筒目录

 8.heroku login 

去Heroku进行通讯

 9.heroku create (add heroku to project) 

9A-问题:当我第一次在Heroku上帐户时,是否应该使用我的应用程序名称创build一个新应用程序,并使用它列出的git说明进行部署? 如果我一开始不应该这样做,那么运行“heroku创造”为我pipe理这个过程?

9B-问题:我只是运行“heroku创build”还是运行“heroku创build-app的名字 – 在这里”?

 10.heroku addons:add bonsai (install bonsai) 

10.问:我需要在Bonasi.io做一个账户,在这之前安装吗?还是为我设立了一个盆景账户? 我从来没有用过盆景。

 11.heroku config (check bonsai instance info and copy your new BONSAI_URL - you will need it later) 

我想这个问题将取决于第10步。

 12.heroku config:set FB_NAME=<instance> FB_TOKEN="<token>" (declare environment variables) 

12.问:什么是Firebase TOKEN? 这是我的Web_API_Key,App_URL或Project_ID? 在我的Firebase控制台中找不到特定于“令牌”的任何内容。 我正在使用Firebase 3。

 13.git add config.js (update) git commit -m "configure bonsai" 

13.提交消息

 14.git push heroku master (deploy to heroku) 

14.推动主人

 15.heroku ps:scale worker=1 (start dyno worker) 

15.问:这是为了什么?

这是一个2部分的答案,第一部分翻过Github的方向,到第19步结束。第二部分扩展了更多不适合第一部分的信息,它将从第19步开始。 我将不得不将其添加到另一个问题,并将其链接到此。

以下是按顺序列出的Github步骤。 仅供参考,我保留原来的步骤与原来的Github author如何列出他们,但在下面我放了一大堆子步骤,每个下面有详细的解释和方向。

假设您已经创build了一个Firebase项目并获取了GoogleService-Info.plist文件

打开你的GoogleService-Info.plist文件。 以下Github directions variablesGoogleService-Info.plist keys

 -FB_NAME is the same thing as your PROJECT_ID -FB_URL is the same thing as your DATABASE_URL -FB_TOKEN is the same thing as your API_KEY So -if your PROJECT_ID is "sneakersearch-az12" then your FB_NAME is "sneakersearch-az12" -if your DATABASE_URL is "https://sneakersearch-az12.firebaseio.com" then your FB_URL is "https://sneakersearch-az12.firebaseio.com" -if your API_KEY is "0012abc789xyz00019" then your FB_TOKEN is "0012abc789xyz00019" //These are not inside your GoogleService-Info.plist but you will encounter them later -FB_SERVICEACCOUNT pertains to downloading a json file from the your project's Firebase console. You will need to go to the page SERVICE ACCOUNTS it's exp in step 3B -clientEmail is the same thing as your Firebase Service Account. You get this from either the Unknown file or on your SERVICE ACCOUNTS page via the FB Console and it's exp in step 3D and 3B -privateKey is the same thing as private_key but this key is NOT your API_KEY, it is a key that is inside the Unknown file from the SERVICE ACCOUNTS page. It looks something like: "-----BEGIN PRIVATE KEY-----\nCYchgacopuyvpc017246t124087t6hpUTYVPUSVDPUCHVEpcl889ljbsiugr4ygrphvuygpuy...mutli-lines...\n-----END PRIVATE KEY-----\n". Also exp in step 3B 

在每个Github步骤的下面,我都有评论和指导。 在我使用下面的值的方向内,你应该使用你的project's values 。 所以对于步骤示例,我将使用这些keysvalues

 //GoogleService-Info.plist PROJECT_ID---aka--FB_NAME: sneakersearch-az12 DATABASE_URL-aka--FB_URL: https://sneakersearch-az12.firebaseio.com API_KEY------aka--FB_TOKEN: 0012abc789xyz00019 //FB Service Account info Firebase service account--aka--clientEmail: firebase-admin-81772@sneakersearch-az12.iam.gserviceaccount.com //this is auto generated for you once you've created your firebase project //Heroku and Bonsai info Heroku Instance Name--aka--Heroku App Name: sneakersearchinstanceAtoZ BONASI_URL --aka--Bonsai Cluster URL: https://abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net 

Swift数据模型:(这将匹配你在步骤19中的映射对象中放置的内容)

 class Sneakers: NSObject{ var sneakercondition: String? var sneakername: String? } 

具有将数据发送到Firebase和searchSnkPath的文件path的VC是一个单独的文件path,盆景将在其上运行search:

 @IBOutlet weak var conditionTextField: UITextField! @IBOutlet weak var nameTextField: UITextField! var dbRef: FIRDatabaseReference! let userID = FIRAuth.auth()?.currentUser?.uid override func viewDidLoad() { super.viewDidLoad() self.dbRef = FIRDatabase.database().reference() } @IBAction func postDataButton(){ var dict = [String:AnyObject]() dict.updateValue(conditionTextField.text!, forKey: "sneakercondition") dict.updateValue(nameTextField.text!, forKey: "sneakername") let idPath = self.dbRef.child("users").child(userID!).child("sneakersPath").childByAutoId() //searches will run on this file path let searchSnkPath = self.dbRef.child("searchSnkPath").childByAutoId() idPath.updateChildValues(dict){ (err, ref) in searchSnkPath.updateChildValues(dict) } } } 

FBDatabase里面,我要search的数据存放在root/searchSnkPath/autoID ,它有两个名为sneakerconditionsneakercondition keys来表示数据。 我想要拉我的search结果的path是root/searchSnkPath

 root | @-users | | | @-userID | | | @-sneakersPath | | | @-autoID | |-sneakercondition | |-sneakername @searchSnkPath | @-autoID |-sneakercondition |-sneakername 

如果我想在searchSnkPath上search并查询这两个keys那么在config.js文件中,我会findexports.paths并在那里设置要search的信息

 exports.paths = [ { path : "https://sneakersearch-az12.firebaseio.com/searchSnkPath", //exp in Step 4 index: "firebase", //exp in Step 17 type : "sneakers", //exp in Step 19 fields: ['sneakercondition', 'sneakername'] //these are the specific keys to search on (exp in Step 4) } ] 

第4步涵盖了所有这一切

在运行git clone https://github.com/firebase/flashlight (步骤2A)之后, 重要的是你需要cdflashlight文件夹中(步骤2B),因为从那一刻开始的所有步骤都发生在flashlight文件夹内,而不是你的主Xcode项目的文件夹。 没有任何东西不能在你的Xcode项目文件夹中工作,因为它没有package.json文件

第1部分 – Github的步骤:

1•通过Heroku安装并运行ElasticSearch或添加盆景服务

1.comments:

Bonsai将在第10步添加

– 在继续之前,您需要按顺序执行以下步骤1A-1E

1.directions:

1A。https://nodejs.org/en/download/下载并安&#x88C5;Node.js

1B。https://devcenter.heroku.com/articles/heroku-cli下载并安&#x88C5;Heroku Toolbelt (已更名为Heroku CLI )。 仅供参考我使用OS X安装程序,而不是OS X Homebrew。

1C。 在您下载并安装Heroku Toolbelt/CLIlogin到Heroku.com并创build一个帐户之后

1D。 创build一个Heroku帐户后,不需要创build一个新的Heroku App您将在步骤9中使用命令行执行此操作。 这很容易

1E。 打开terminal并运行: node -v查找当前正在运行的node version (例如,您获得v6.9.1 )。 在第二步你将不得不确保从那里的版本匹配这个输出。

2•git clone https://github.com/firebase/flashlight

2.comments:

– 假设Xcode项目位于名为sneakerSearchFolder的文件夹内的桌面上

– 在terminal你cd到sneakerSearchFolder文件夹 – 即运行: cd Desktop/sneakerSearchFolder

– 在sneakerSearchFolder中,您通过https://github.com/firebase/flashlight clone the github repo

重要你然后cdflashlight文件夹即运行: cd flashlight

– 您将需要根据https://devcenter.heroku.com/articles/deploying-nodejs运行&#x7684;node version

-run: node -v当你制作一个Heroku instance你可以根据上面的devcenter.heroku链接获取在flashlight文件夹(ie v6.9.1 )里面运行的node version

– 如果您在flashlight文件夹中,如果您刚刚运行的node version与步骤1E中的版本不匹配,则需要更新手电筒文件夹内的版本以使其匹配。 将Node.js升级到最新版本 如果两个版本匹配,则不必担心这一点

– 假设您的节点版本匹配,打开您的flashlight文件夹中的package.json文件,并添加一个"engines"对象,其键值是您当前使用的node version

 "engines": { "node": "node_version_num_you_got_back_from_running_node -v" } 

2.directions:

2A。 在terminal导航到您的Xcode项目所在的文件夹

2B。 运行: git clone https://github.com/firebase/flashlight

2C。 运行: cd flashlight

2D。 运行: node -v

2E。 打开flashlight文件夹,然后打开package.json 。 将以下内容添加到文件中。

 "engines": { "node": "whatever_was_returned_from_Step_2D" } 

2E-例子。 仅供参考,这是一个例子。 打开package.json并在"dependencies" closing brace后面添加它。 在大括号后面必须加上一个逗号。 并且不要在版本号之前添加“v”。 保存文件。

 "dependencies": { "JQDeferred": "~1.9.1", "colors": "~0.6.2", "elasticsearch": "^11.0.1", "firebase": "^3.5.2" }, "engines": { "node": "6.9.1" } 

3•npm安装

从这个angular度来看,你的flashlight文件夹里面的东西是非常重要的,而不是你的主要项目的文件夹中的一切工作,否则你会得到错误

3.comments:

– 你应该仍然在你的flashlight文件夹

– login到FB项目控制台内的Firebase ,进入Project's SettingsOverview旁边的小圆图标),在项目设置中selectSERVICE ACCOUNTS ,进入Firebase Admin SDK部分。 有两件事你必须在这里做。 1.find并复制您的Firebase service account ,它看起来像firebase-admin-81772@sneakersearch-az12.iam.gserviceaccount.com和2.在页面底部,您将必须单击Generate New Private Keybutton,它将下载一个Unknown file ,您将需要重命名为service-account.json 。 如果它将文件命名为Unknown以外的其他文件,只需将其重命名为service-account.json 。 将文件重命名后,将其拖到flashlight文件夹中,因为步骤4B的FB_SERVICEACCOUNT需要从那里访问该文件。 请FLASHLIGHT FOLDER

– 这不是在github步骤中列出,但它是必要的。 您必须将Firebase Server SDK Credentials添加到您的项目

– 您需要在flashlight文件夹中运行$ npm install firebase-admin --save命令,否则会出现错误,因为它将查找packae.json文件。 该文件已经在您的flashlight文件夹中,而不是在您的主Xcode项目文件夹中

按照https://firebase.google.com/docs/server/setup&#x5728;Initialize the SDK部分中的说明操作。 您将需要2个来自Unknown文件(现在应该重命名为service-account.json )的值来初始化它。 这些值在第5行private_key和第6 clientEmail 。 仅供参考clientEmailFirebase service account是一回事

– 这里是你在SDKinitializing的内容:

 var admin = require("firebase-admin"); //this imports the npm firebase-admin module you just installed admin.initializeApp({ credential: admin.credential.cert({ projectId: "<PROJECT_ID>", //projectId: is the PROJECT_ID from your GoogleService-Info.plist file clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com", //clientEmail: is on line 6 in the Unknown file which is also your "Firebase service account" info privateKey: "-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n" //privateKey: is NOT your API_KEY/FB_TOKEN. Inside the Unknown file on line 5 there is a very long multiline "private_key" key. It looks something like "-----BEGIN PRIVATE KEY-----\nCYchgacopuyvpc017tEpcl889ljbsiugr4ygrphvuygpuy...mutli-lines...\n-----END PRIVATE KEY-----\n". You need to copy and paste it from there to here. Be sure to include the "-----BEGIN PRIVATE KEY-----\n and \n-----END PRIVATE KEY-----\n }), databaseURL: "https://<DATABASE_NAME>.firebaseio.com" //databaseURL: is the DATABASE_URL from your GoogleService-Info.plist file }); 

-FYI在Initialize the SDK部分中,您还可以select使用顶部的path/to/serviceAccountKey.json而您可以将path提供给重命名的未知文件。 我select底部inline部分,因为它更容易。 如果你遵循这些步骤,你不需要担心这个。

– 在flashlight文件夹中有一个app.js文件,复制并粘贴上面的代码,并把它放在文件的顶部

在命令行terminal里面运行: npm install

– 如果一切都好,唯一的警告你应该是No repository fieldNo license field

3.directions:

3A 。 确保你的flashlight文件夹里面运行: pwd

3B。 login到您的Firebase ConsoleSERVICE ACCOUNTS页面,然后单击“ Generate New Private Keybutton以下载Unknown文件。

3C。Unknown文件重命名为service-account.json ,并将该文件放在flashlight文件夹中

3D。service-account.json文件中,复制名为client_emailkey或者您可以复制Firebase SERVICE ACCOUNTS页面上的服务帐户信息

3E。 在terminal里面运行: npm install firebase-admin --save

3F。 复制Initialize the SDK并使用以下值初始化字段:

 var admin = require("firebase-admin"); admin.initializeApp({ credential: admin.credential.cert({ projectId: "sneakersearch-az12", //use your PROJECT_ID clientEmail: "firebase-admin-81772@sneakersearch-az12.iam.gserviceaccount.com", //clientEmail: is on line 6 in the Unknown file privateKey: "-----BEGIN PRIVATE KEY-----\nCYchgacopuyvpc017tEpcl889ljbsiugr4ygrphvuygpuy...mutli-lines...\n-----END PRIVATE KEY-----\n" //privateKey: is on line 5 in the Unknown file }), databaseURL: "https://sneakersearch-az12.firebaseio.com" //use your DATABASE_URL }); 

3G。flashlight文件夹中,打开app.js文件,用正确的值粘贴/保存上面的代码

3H。 运行: npm install

4•编辑config.js(请参阅顶部的注释,您必须至less设置FB_URL和FB_SERVICEACCOUNT)

4.comments:

– 在flashlight文件夹中有一个名为config.example.js的文件,将其打开

– 在这个config.example.js文件中,您可以findFB_URLFB_SERVICEACCOUNTvariables(它们在第13行和第23行中列出)

– 您需要更改exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com'; exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com';exports.FB_URL = process.env.FB_URL || 'whatever_your_DATABASE_URL_is'; exports.FB_URL = process.env.FB_URL || 'whatever_your_DATABASE_URL_is'; (一定要把whatever_your_DATABASE_URL_is放在单引号或双引号内)

在第23行exports.FB_SERVICEACCOUNT = process.env.FB_ACC || 'service-account.json'; exports.FB_SERVICEACCOUNT = process.env.FB_ACC || 'service-account.json'; 是用来访问步骤3C中的service-account.json file (这里不需要添加或改变任何东西,因为它可以自己访问它)

– 您现在必须设置数据所在的path(如上面的ViewController中用于将数据发送到FB的searchPath ),索引(步骤17)以及所需的types(类似于您的数据模型)在线监测64,65和66

 exports.paths = [ { path : "users",//line 64 index: "firebase",//line 65 type : "user"//line 66 }, 

path是你的dataFB内部的位置。 这是您要从<DATABASE_URL>/searchSnkPathsearch结果的<DATABASE_URL>/searchSnkPath

– 步骤17中的index exp

– 在步骤19中 type exp

-FYI第69-79行将build立你想要监视的另一条path。 如果需要,您可以删除或注释这些。 你也可以创build更多的path来监视,即如果你有一个path<DATABASE_URL>/searchClothingPath那么你也可以设置search。 你也可以添加更多的path,如果你想,即:

 exports.paths = [ { path : "https://sneakersearch-az12.firebaseio.com/searchSnkPath" index: "firebase", type : "sneakers" }, { path : "<DATABASE_URL>/searchClothingPath" index: "firebase", type : "clothingDataModel", fields: ['jeans','shirts']//completely optional to use }, { path : "<DATABASE_URL>/searchHatsPath" index: "firebase", type : "hatDataModel", }, //etc... 

-FYI Fields是将在ES中索引的keys 。 这是一个可选的东西来添加。 如果你有10个keys ,只想要其中2个被indexed那么你会添加这个。 这10个键只有那2个keys才能被search到。 你也可以使用config.js文件中的parsing函数来做同样的事情

– 之后保存/closuresconfig.example.js并将其重命名为config.js

4.directions:

4A。 打开config.example.js文件并在第13行更改exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com'; exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com';exports.FB_URL = process.env.FB_URL || 'https://sneakersearch-az12.firebaseio.com'; exports.FB_URL = process.env.FB_URL || 'https://sneakersearch-az12.firebaseio.com';

4B。 第23行是用来访问service-account.json文件的(只要在步骤3C中重命名Unknown file就行了)

4C。 第64,65,66行是我想要监视的,在第67行,我添加了两个我想search的Firebase Database Keysfields key ,虽然没有必要

 exports.paths = [ { path : "https://sneakersearch-az12.firebaseio.com/searchSnkPath",//line 64 index: "firebase",//line 65 type : "sneakers",//line 66 fields: ['sneakercondition', 'sneakername']//line 67 }, 

4D。 保存/closuresconfig.example.js文件。 现在重命名文件config.js

5•节点app.js(运行该应用程序)

5.comments:

– 你将不会运行这个命令,直到最后,所以现在跳过它,但有一些你应该知道的。 正在运行的node app.js在您的本地机器上运行您的Heroku应用程序。 它会在本地寻找你没有的ElasticSearch,所以你会得到连接错误。 如果你想在本地运行,那么你将不得不在步骤12B-Optional中运行export BONSAI_URL="<your_bonsai_url>"代码,这是解决本地连接错误所需要的。 如果您的应用程序睡着了/崩溃(exp接近结束),您将不得不再次运行export BONSAI_URL="<your_bonsai_url>"命令。 除此之外,Heroku将自动运行应用程序,当你推动代码。

– 现在这一步你需要打开你的app.js文件,并根据https://docs.bonsai.io/docs/nodejs添加一些代码&#x3002; 您应该在var elasticsearch = require('elasticsearch')声明之后的任何地方添加代码。

 /* this code is already inside the app.js file. You should add the code anywhere below it var elasticsearch = require('elasticsearch'), conf = require('./config'), fbutil = require('./lib/fbutil'), PathMonitor = require('./lib/PathMonitor'), SearchQueue = require('./lib/SearchQueue'); */ //You need to add var bonsai_url = process.env.BONSAI_URL; var client = new elasticsearch.Client({ host: bonsai_url, log: 'trace' }); // Test the connection... client.ping({ requestTimeout: 30000, hello: "elasticsearch" }, function (error) { if (error) { console.error('>>>My Bonsai url is>>>' + bonsai_url) console.error('>>>Elasticsearch cluster is down!'); } else { console.log('All is well'); } } ); 

– 方向也说运行$ export BONSAI_URL="https://username:password@my-awesome-cluster-123.us-east-1.bonsai.io" (换句话说export BONSAI_URL="<your_BONSAI_URL>" )如果你想在本地运行你的应用程序来使用ES,你将会这样做。 这不是意味着远程

5.directions:

5.打开app.js文件并添加下面的代码并保存文件:

 var bonsai_url = process.env.BONSAI_URL; var client = new elasticsearch.Client({ host: bonsai_url, log: 'trace' }); // Test the connection... client.ping({ requestTimeout: 30000, hello: "elasticsearch" }, function (error) { if (error) { console.error('>>>My Bonsai url is>>>' + bonsai_url) console.error('>>>Elasticsearch cluster is down!'); } else { console.log('All is well'); } } ); 

6•curl -X POST http:// localhost:9200 / firebase

6.comments:

– 跳过这一步,因为在这一点上,你的cpu没有本地ES来索引

6.directions:

6.跳过这一步

7•cd手电筒

7.comments:

– 不需要运行这个,你应该仍然在你的flashlight文件夹

7.directions:

7.跳过这一步

8•herokulogin

8.comments:

– 打开terminal

– 您将不得不input您的Herokuemail addresspassword. input密码时,它将显示为空白

8.directions:

8A。 运行: heroku login

8B。 在提示input您的email addresspasswordlogin到Heroku

9•heroku创build(添加heroku项目)

9.comments:

这就是为什么你不需要在步骤1D创build一个heroku应用程序。 现在您将创build并命名您的heroku应用程序

– input完成后,你需要input你想要的名字。 即我select名称sneakersearchinstanceAtoZ

– 如果您login到Heroku运行此命令,您应该看到列出的应用程序的名称。 转到右上angular,点击9 dots ,selectDashboard

9.directions:

9.运行: heroku create sneakersearchinstanceAtoZ

10•heroku插件:添加盆景(安装盆景)

10.comments:

– 您需要在您的Heroku账户中添加一个credit card ,然后再运行此步骤,否则在您运行之后会说您必须这样做。 这仍然是一个免费的计划。 我不确定是否可以添加Bonsai add on没有卡Bonsai add on

– 要添加信用卡,请点击右侧的圆形图标,selectAccount Settings ,然后selectBilling

– 你现在需要添加bonsai到你的heroku app 。 你可以通过运行heroku addons:add bonsai --app <your-app-name>

<your-app-name>是在步骤9中创build的heroku应用程序的名称

– 创build一个bonsai cluster它会要求您通过运行heroku addons:open bonsai集群heroku addons:open bonsai

10.directions:

10A。 login到Heroku并添加信用卡到Billing Page

10B。 运行: heroku addons:add bonsai --app sneakersearchinstanceAtoZ第9步相同的确切名称

10C。 运行heroku addons:open bonsai

11•herokuconfiguration(检查盆景实例信息,并复制您的新BONSAI_URL你将需要它)

11.comments:

– 运行heroku config将获得您所有的environment variables及其相应的值。 当你看到你的bonsai's url ,复制它的价值,你会需要它的步骤12B和17

– 这是从步骤10C将会看到完全相同的url

bonsai url可能类似于https://abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net

11.directions:

11A。 运行: heroku config

11B。 复制返回的bonsai url

•heroku config:设置FB_NAME = FB_TOKEN =“”(声明环境variables)

12.comments:

-12A是必需的。 您需要使用项目的GoogleService-Info.plistPROJECT_ID这是FB_NAMEAPI_KEY (这一步是FB_TOKEN

-12B是可选的。 如果您想在本地连接到ElasticSearch ,请运行此操作。 你将不得不设置你的bonsai url以便运行node app.js不会抛出连接错误。 你可以通过运行: export BONSAI_URL="<your_bonsai_url>" 。 您将需要运行此命令每个terminal会话或如果您的Heroku应用程序睡着了/崩溃。

– 不要在等号之前或之后使用空格

12.directions:

图12A(必需)。 运行: heroku config:set FB_NAME=sneakersearch-az12 FB_TOKEN=0012abc789xyz00019

12B(可选) .run: export BONSAI_URL="https://abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net"

13•git add config.js(更新)

13.comments:

– 如果你运行这个,你可能会得到一个错误,说: 你的.gitignore文件忽略了下面的path:config.js如果你真的想添加它们,请使用-f。

– 这是说运行git add config.js然后在最后加上-f

13.directions:

13A。 运行: git add config.js

13B。 如果运行有错误: git add config.js -f

git commit -m“configure bonsai”

14.directions:

14.运行: git commit -m "configure bonsai"

15•git push heroku master(部署到heroku)

15.directions:

15.运行: git push heroku master

16•heroku ps:规模工人= 1(开始工作人员)

16.comments:

– 这个命令的作用在这里的答案有人可以解释“heroku ps:scale web = 1”

16.directions:

16.运行: heroku ps:scale worker=1

17•curl -X POST / firebase(例如: https://用户名:password@yourbonsai.bonsai.io/firebase )

17.comments:

在开始这个步骤之前,请阅读@DoesData更新的答案(下面的地雷)来使用curl -X PUT来代替

– 抓取您从第11B步复制的bonsai url

curl -X POST命令之后粘贴URL,并确保将/firebase添加到结尾

这个function会创build一个名为/firebase的索引,指向您的bonsai url 。 打开你的config.js文件并查找exports.paths (第62行)。 它有一个名为index: "firebase"的键/值对(第65行),该值指向刚创build的索引并从那里访问ES集群

– 基本上在你的config.js文件中,第65行的indexfirebase必须与你添加到最后curl -X POST命令的名字/firebase完全匹配。 如果名称不匹配,则不会有效。

如果成功,你应该得到这个回应: {"acknowledged":true}

17.directions:

17.运行: curl -X POST https://abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net/firebase

18•现在可以使用步骤5节点app.js(运行应用程序)

18.comments:

– 运行这个命令将在本地启动应用程序。 您也可以运行npm start但是无论哪种方式,它都会在本地机器上启动它,您将需要在步骤12B(可选)中运行代码以使其工作

18.directions

18.运行: node app.js

•设置映射对象(s)

供参考步骤十九是非常重要的。 你必须设置你的映射对象,它是关键。 我没有足够的字符在这里详细解释。 这是第4步的type键用于设置。 观看此video以说明如何设置您的mappings object https : //www.youtube.com/watch?v=h3i3pqwjtjA&feature=youtu.be

19.directions:

19A。 – 打开TextEditSublime然后创build一个文件并命名为sneakerfile.json 。 保存并将文件拖到flashlight文件夹中。 您需要.json扩展名。

19B。 – 在该文件中添加以下代码,保存并closures文件:

 { "sneakers": { "properties": { "sneakercondition": { "type": "string" }, "sneakername": { "type": "string" } } } } //notice this is just like the Swift Sneakers Data Model declared at the beginning 

19C。 抓住你的BONSAI_URL运行heroku config:get BONSAI_URL 。 我的BONSAI_URLhttps:// abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net

19D。 运行: curl -XPOST https://abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net/firebase/sneakers/_mapping -d@sneakerfile.json

19E。 运行: curl -XGET <BONASI_URL>/firebase/sneakers/_mapping?pretty

第1部分完成。第2部分将详细解释步骤19

进一步信息 –

Heroku命令:

检查您的dyno

运行: heroku ps

如果你得到以下内容,请参阅下面的链接

 === web (Free): npm start (1) web.1: crashed 2017/01/01 12:00:00 -0500 (~ 38m ago) === worker (Free): node ./app.js (1) worker.1: crashed 2017/01/01 12:00:00 -0500 (~ 10m ago) 

在创build你的Heroku app你的dyno是在Heroku Free Plan 。 如果在一定的时间内没有被使用,它将最终fall asleep / crash 。 停止睡眠/碰撞后续

简单的方法来防止Heroku闲置?

https://devcenter.heroku.com/articles/free-dyno-hours

https://devcenter.heroku.com/articles/dyno-sleeping

其他命令:

 heroku help //help heroku status //Heroku platform status heroku logs //displays 100 logs heroku logs --tail //realtime logs heroku logs --tail | grep worker //dyno worker logs heroku ps -a <heroku app name> //how many dyno hrs you have left heroku config:get BONSAI_URL //gets only your bonsai url heroku config //all your environment variables heroku apps:info //your Heroku credentials 

要回到 command prompt Ctrl+C

您也可以联系Heroku支持或盆景支持,因为它们非常有帮助

我将很快发布第2部分。

上面的答案很好。 然而,我的更新有太多的评论是有用的,所以我把它放在这里。

如果您按照上述步骤操作,则会在步骤中遇到错误:

  1. 运行:curl -X POST https:// abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net/firebase

较新版本的elasticSearch不再允许您使用POST创build新索引,但可以使用PUT。 所以你想运行

 curl -X PUT https://abc123a01:01abc12de45xyz34@xyz-012345.us-east-1.bonsaisearch.net/firebase 

答案应该是: {"acknowledged":true,"shards_acknowledged":true}

看到这个问题的更多信息。

更新

我也转到了阿尔戈利亚,因为文件更清楚了。 但是,我会说弹性search文档急需更新。 我build议那些对弹性search感兴趣的人尝试使用Firebase云function而不是Heroku来部署他们的代码。 我一直无法find这方面的指导,但是node.js代码应该是相似的,除了它将被部署到Firebase云而不是Heroku。 这应该使这个过程简单得多。 您可以在这里查看Firebase云端function