Using config module in NodeJs returning 'undefined' - node.js

I was trying to run the sample here. He didn't show how he stored his config values. Based on the doc https://docs.npmjs.com/cli/config, I did the following:
npm install config
npm config set database Demo1
npm config list
The list showed the variable set. But in node, I tried this:
var config = require("config") ;
console.log("config.database=" + config.database);
Which shows the output:
config.database=undefined
Part 2 - would this config module work well in an AWS Lambda environment as well? I'm testing on Windows right now.

Got it now. I didn't catch the difference between
https://www.npmjs.com/package/config
and
https://docs.npmjs.com/cli/config.
With the second one, I created in my project folder a config directory and within that a file called default.json, and put the text below in that file.
{
"database": "MyDBName"
}
Obviously, I'll add the other parms, but just wanted to see it work with one parm first.
I can see this being compatible with AWS Lambda since it is just a JSON file. And has further capabiltiies of nested names/values, arrays, etc...

Related

how to setup multiple paths in user environmental variables in windows 10

I want to set up multiple paths in the user environmental variables in windows 10. but can only set a single path, how to overcome this issue?
If you want to add user defined environment variable then use react-native-config library. So in short if there are variables defined in .env file then you can use it anywhere in the react-native app like below:-
.env file
API_URL = "https://baseurl/endpoint"
//To use the above environment variable
import config react-native-config
function apiCall() { axios(config) }
With the help of react-native-config you can use environment variable in native code also and vise-versa.
For better use define .env.staging file. By default config lib finds .env file. To run the application with .env.staging file see below command.
$ ENVFILE=.env.staging react-native run-ios # bash
$ SET ENVFILE=.env.staging && react-native run-ios # windows
$ env:ENVFILE=".env.staging"; react-native run-ios # powershell
For more information checkout :- https://github.com/luggit/react-native-config
Happy coding mate :)
to add multiple paths for a single environment variable, list them all with a semicolon(";") in between like that:
C:\Cpp_Headers\pybind11;C:\Cpp_Headers\cpython\Include;C:\Users\admin\Cpp_Headers\cpython\PC
(and so forth). From this input Windows will automatically recognize multiple definitions.

How to interpolate environment variables within netlify.toml config

I want to proxy to a different api depending on the environment - I've tried a few variations on the following theme without any luck. What's the correct way of doing this, if its even possible?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
This does not work.
Although the above config works when I hardcode a URI into the to field, it just fails when I try to interpolate an env var.
It's not supported, but Netlify suggest a work-around in their documentation (https://www.netlify.com/docs/netlify-toml-reference):
Using Environment Variables directly as values ($VARIABLENAME) in your
netlify.toml file is not supported. However, the following workflow
can be used to substitute values based on environment variables in the
file, assuming you are only trying to change headers or redirects. The
rest of the file is read BEFORE your build - but those sections are
read AFTER the build process.
Add a placeholder like
API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers
sections.
Create an Build Environment Variable, for example API_KEY,
with the desired value. You can do this in the toml file or in our UI
in the Build and Deploy Settings section of your configuration. You
might use the latter to keep sensitive values out of your repository.
Add a command like this one to your build command: sed -i
s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.
Answering my own question - it's not supported, you have to manually interpolate env vars yourself as part of the build on Netlify.
Yes. It's possible. Here is the detailed docs: https://www.netlify.com/docs/continuous-deployment/#deploy-contexts
In my case, I need to set a REACT_APP_API_URL separate for production and all other branches. Here is what I use:
[context.production.environment]
REACT_APP_API_URL = "https://api.test.im"
[context.deploy-preview.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
[context.branch-deploy.environment]
REACT_APP_API_URL = "https://api-staging.test.im"

iOS Firebase Flashlight/ElasticSearch Heroku Setup using Node.JS

I'm building an iOS app in Swift with a Firebase backend. I need advanced search options for my app. I've been told I should use Flashlight/ElasticSearch via this github link https://github.com/firebase/flashlight
Assume my app's name is SneakerSearch and my
Firebase Project_ID is- sneakersearch-az12
Firebase Web_API_Key is- abc123XYZ000...
Firebase App_Url is- gs://sneakersearch-az12.appspot.com
I need some advice with the steps for setting this up as I'm new to Heroku and Node.js. I never learned either before but I've already installed the Heroku tool belt and 'sudo gem heroku install' is done.
I've listed the github directions/steps and the the step(s) I took for each direction. I need assistance with directions: 1,4,5,6,9,10,12, and 15.
1.Install and run ElasticSearch or add Bonsai service via Heroku
1. I make an account at Heroku.
1.-QUESTION: Do I need to create the name of my app and deploy it after I've setup my Heroku account or should I wait until step #9?
2.git clone https://github.com/firebase/flashlight
2.I clone the above into my iOS app's project folder
3.npm install
3.At the prompt I first I run "npm init" then I run "npm install"
4. edit config.js (see comments at the top, you must set FB_URL and FB_SERVICEACCOUNT at a minimum)
4A.-QUESTION: What is "FB_SERVICEACCOUNT"? I'm using Firebase 3 and I couldn't find anything on a service account in my console. Is this another Firebase account that I have to setup for my project using node.js in addition to the Firebase swift project I've already made?
4B.-QUESTION: Where do I "edit config.js" and set my FB_URL and FB_SERVICEACCOUNT at? Should this be in my package.json file?
5.node app.js (run the app)
5.-QUESTION: In terminal I ran "node app.js" and got "throw err;^". Why did I get this error?
6.curl -X POST http://localhost:9200/firebase
6.-QUESTION: What is this for?
7.cd flashlight
7.switched to the flashlight dir
8.heroku login
8.logged in to Heroku
9.heroku create (add heroku to project)
9A-QUESTION: When I first made my account at Heroku should I have created a new app with my app's name and deployed it using the git instructions it has listed? If I should not have done that at first will running "heroku create" manage that process for me?
9B-QUESTION: Do I just run "heroku create" or do I run "heroku create -app's name- here"?
10.heroku addons:add bonsai (install bonsai)
10.-QUESTION: Do I need to make an account at Bonasi.io and install it before this step or does this set up a bonsai account for me? I've never used bonsai before.
11.heroku config (check bonsai instance info and copy your new BONSAI_URL - you will need it later)
11.I guess this question would depend on step #10.
12.heroku config:set FB_NAME=<instance> FB_TOKEN="<token>" (declare environment variables)
12.-QUESTION: What is the Firebase TOKEN? Is this my Web_API_Key, App_URL, or Project_ID? I could not find anything specific to "token" inside my firebase console. I'm using Firebase 3.
13.git add config.js (update)
git commit -m "configure bonsai"
13. commit with message
14.git push heroku master (deploy to heroku)
14. push to master
15.heroku ps:scale worker=1 (start dyno worker)
15.-QUESTION: What is this for?
This is a 2 part answer with the 1st part going over the Github directions and ending on Step 19. The 2nd part extends more info that couldn't fit into the 1st part and it will begin on step 19 in detail. I'll have to add it to another question and link it to this.
Here are the Github steps listed in order. FYI I kept the original steps in line with how the original Github author listed them but underneath I put a bunch of sub-steps with detailed explanations and directions under each one.
Assuming you already created a Firebase project and got your GoogleService-Info.plist file
Open your GoogleService-Info.plist file. The following variables from the Github directions correlate with GoogleService-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
Underneath each Github step I have comments and then directions. Inside the directions where I use the below values you should instead use your project's values. So for the step examples I'll use these keys and values:
//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 Data Model: (this will match what you put in your mappings object in Step 19)
class Sneakers: NSObject{
var sneakercondition: String?
var sneakername: String?
}
VC with file paths where I send data to Firebase and searchSnkPath is a separate file path where Bonsai will run it's searches on:
#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)
}
}
}
Inside FBDatabase the path where my data to search is stored at root/searchSnkPath/autoID and it has 2 keys named sneakercondition and sneakername that represents the data. The path I want to pull my search results from is root/searchSnkPath
root
|
#-users
| |
| #-userID
| |
| #-sneakersPath
| |
| #-autoID
| |-sneakercondition
| |-sneakername
#searchSnkPath
|
#-autoID
|-sneakercondition
|-sneakername
If I want to search on the searchSnkPath and query on those 2 keys then inside the config.js file I'd find exports.paths and inside there is where I would set the info to search on
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)
}
]
Step 4 covers all this
Important after you run git clone https://github.com/firebase/flashlight (step 2A) you need to cd into the flashlight folder (step 2B) as ALL the steps from that point on happen inside the flashlight folder and NOT your main Xcode project's folder. Nothing will not work inside your Xcode project's folder because it doesn't have a package.json file
Part 1 - Github steps:
1•Install and run ElasticSearch or add Bonsai service via Heroku
1.comments:
-Bonsai will get added at step 10
-You DO need to have the following steps 1A-1E in order before you proceed
1.directions:
1A. Download and install Node.js from https://nodejs.org/en/download/
1B. Download and install the Heroku Toolbelt (it's been renamed Heroku CLI) at https://devcenter.heroku.com/articles/heroku-cli. FYI I used the OS X Installer and not OS X Homebrew.
1C. After you've downloaded and installed the Heroku Toolbelt/CLI log on to Heroku.com and create an account
1D. After creating a Heroku account no need to create a new Heroku App You will do this in step 9 using the command line. It's very easy
1E. Open terminal and run: node -v to find out the current node version your running (for example you get v6.9.1). In step 2D you will have to make sure the version from there matches this output.
2•git clone https://github.com/firebase/flashlight
2.comments:
-Assume the Xcode project lives on the desktop inside a folder named sneakerSearchFolder
-Inside terminal you cd to the sneakerSearchFolder folder- i.e. run: cd Desktop/sneakerSearchFolder
-Once inside the sneakerSearchFolder you clone the github repo at https://github.com/firebase/flashlight
-Important You then cd to flashlight folder i.e. run: cd flashlight
-You will need the node version your running according to https://devcenter.heroku.com/articles/deploying-nodejs
-run: node -v to get what node version is running inside the flashlight folder (i.e v6.9.1) later on when you make a Heroku instance you will need this according to the devcenter.heroku link above
-Now that your inside the flashlight folder, if the node version you just ran doesn't match the version from step 1E, you will need to update the version inside the flashlight folder to make them match. Upgrading Node.js to latest version If both versions match then you don't have to worry about this
-Assuming your node versions match, Open the package.json file inside your flashlight folder and add an "engines" object with the key's value being the node version you are currently using
"engines": {
"node": "node_version_num_you_got_back_from_running_node -v"
}
2.directions:
2A. in terminal navigate to whatever folder your Xcode project is in
2B. run: git clone https://github.com/firebase/flashlight
2C. run: cd flashlight
2D. run: node -v
2E. Open the flashlight folder then open package.json. Add the following to the file.
"engines": {
"node": "whatever_was_returned_from_Step_2D"
}
2E-example. FYI here's an example. Open package.json and after the "dependencies" closing brace, add it there. BE SURE TO ADD a COMMA after the closing brace. And don't add the "v" before the version number. Save the file.
"dependencies": {
"JQDeferred": "~1.9.1",
"colors": "~0.6.2",
"elasticsearch": "^11.0.1",
"firebase": "^3.5.2"
},
"engines": {
"node": "6.9.1"
}
3•npm install
From this point on it's very important that your inside your flashlight folder and not your main project's folder for everything to work otherwise you will get errors
3.comments:
-You should still be inside your flashlight folder
-Log into Firebase, inside the console of your FB project, go to the Project's Settings (little round icon next to Overview), inside Project Settings choose SERVICE ACCOUNTS, go to the section Firebase Admin SDK. There are 2 things you have to do here. 1. Find and copy your Firebase service account, it looks like firebase-admin-81772#sneakersearch-az12.iam.gserviceaccount.com and 2. at the bottom of the page you will have to click the Generate New Private Key button, it will download an Unknown file that you will need to rename to service-account.json. If it names the file something besides Unknown just rename that to service-account.json. After you rename the file drag it into your flashlight folder because Step 4B FB_SERVICEACCOUNT will need to access this file from there. MAKE SURE YOU PUT THE FILE INSIDE THE FLASHLIGHT FOLDER!
-This isn't listed in the github steps but it's necessary. You must add Firebase Server SDK Credentials to your project
-You will need to be inside your flashlight folder to run the $npm install firebase-admin --save command or you'll get errors because it will look for a packae.json file. The file is already inside your flashlight folder and not inside your main Xcode project's folder
-Follow the directions from https://firebase.google.com/docs/server/setup, inside the Initialize the SDK section. You will need 2 of the values from the Unknown file (which should now be renamed service-account.json) to initialize it. The values are on lines 5-private_key and 6-clientEmail. FYI the clientEmail and Firebase service account are the same thing
-Here's what you are initializing inside the SDK:
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 inside the Initialize the SDK section you also have the option of using the top part where it says path/to/serviceAccountKey.json and you can instead supply the path to the renamed Unknown file. I choose the bottom inline part as it was easier. If your following these steps you don't need to worry about this though.
-Inside the flashlight folder there is an app.js file, copy and paste the above code and put it at the top of the file
-Back inside terminal on the command line run: npm install
-If everything is all good the only warnings you should have are No repository field and No license field
3.directions:
3A. Make sure your inside the flashlight folder run: pwd
3B. Log into your Firebase Console's SERVICE ACCOUNTS page and click the Generate New Private Key button to download an Unknown file.
3C. Rename the Unknown file to service-account.json and put the file inside your flashlight folder
3D. Inside the service-account.json file copy the key named client_email or you can copy the service account info on your Firebase SERVICE ACCOUNTS page
3E. Inside terminal run: npm install firebase-admin --save
3F. Copy what's inside the section Initialize the SDK and initialize the fields with these values:
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. Inside the flashlight folder, open the app.js file, paste/save the above code inside of it with the correct values
3H. run: npm install
4•edit config.js (see comments at the top, you must set FB_URL and
FB_SERVICEACCOUNT at a minimum)
4.comments:
-Inside your flashlight folder there is a file named config.example.js, open it
-Inside this config.example.js file is where you find the FB_URL and FB_SERVICEACCOUNT variables (they are listed on lines 13 and 23)
-You need to change exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com'; to exports.FB_URL = process.env.FB_URL || 'whatever_your_DATABASE_URL_is'; (be sure to put whatever_your_DATABASE_URL_is inside single or double quotes)
-on line 23 exports.FB_SERVICEACCOUNT = process.env.FB_ACC || 'service-account.json'; is what is used to access the service-account.json file from step 3C (there is no need to add or change anything here as it will access it on it's own)
-You will now have to setup the path where your data sits at (like the searchPath used to send data to FB inside the ViewController above), the index (Step 17), and the type (similar to your data model) that you want to monitor on line 64,65, and 66
exports.paths = [
{
path : "users",//line 64
index: "firebase",//line 65
type : "user"//line 66
},
-path is where your data sits at inside FB. It's where you want to pull search results from i.e. <DATABASE_URL>/searchSnkPath
-index exp in Step 17
-type exp in Step 19
-FYI lines 69-79 would set up another path that you would want to monitor. You can delete or comment these out if you want. You can also create more paths to monitor i.e. If you had a path <DATABASE_URL>/searchClothingPath then you would set up search on that also. You can also add more paths if you want to i.e:
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 are the keys that will be indexed in ES. This is an optional thing to add. If you had 10 keys and only wanted 2 of them to be indexed then you would add this. This means of the 10 keys only those 2 keys would be searchable. You can also use the parse function inside the config.js file to do the same thing
-Afterwards save/close the config.example.js and rename it config.js
4.directions:
4A. Open the config.example.js file and on line 13 change exports.FB_URL = process.env.FB_URL || 'https://<YOUR APP>.firebaseio.com'; to exports.FB_URL = process.env.FB_URL || 'https://sneakersearch-az12.firebaseio.com';
4B. Line 23 is what is used to access the service-account.json file (as long as you renamed the Unknown file in step 3C just move on)
4C. Line 64,65,66, are what I want to monitor and on line 67 I added the fields key for the two Firebase Database Keys I want to search on although it is not necessary
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. Save/close the config.example.js file. Now rename the file config.js.
5•node app.js (run the app)
5.comments:
-You won't run this command until the very end so skip it for now but there is something you should know. Running node app.js runs your Heroku app on your local machine. It will look for ElasticSearch locally which you don't have so you will get connection errors. If you want to run it locally then you will have to run the export BONSAI_URL="<your_bonsai_url>" code at Step 12B-Optional which is needed to resolve local connection errors. If your app falls asleep/crashes (exp near the end) you will have to run the export BONSAI_URL="<your_bonsai_url>" command again. Other then that Heroku will run the app automatically when you push code up to it.
-As of now for this step you need to open your app.js file and according to https://docs.bonsai.io/docs/nodejs add some code. You should add the code anywhere after the var elasticsearch = require('elasticsearch') declaration.
/*
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');
}
}
);
-The directions also says to run $ export BONSAI_URL="https://username:password#my-awesome-cluster-123.us-east-1.bonsai.io" (in other words export BONSAI_URL="<your_BONSAI_URL>") which is what you would do ONLY if you want to run your app locally to use ES. It's not meant for remote
5.directions:
5. Open the app.js file and add the code below and save the file:
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:
-Skip this step because at this point you don't have a local ES on your cpu to index
6.directions:
6. SKIP THIS STEP
7•cd flashlight
7.comments:
-No need to run this, you should still be inside your flashlight folder
7.directions:
7. SKIP THIS STEP
8•heroku login
8.comments:
-Open terminal
-You will have to enter your Heroku's email address and password. As you enter the password it will show up blank
8.directions:
8A. run: heroku login
8B. At the prompt enter your email address and password to login into Heroku
9•heroku create (add heroku to project)
9.comments:
-This is why you didn't need to create a heroku app at step 1D. Now you will create and name your heroku app
-After you type in create you need to type in whatever name you want your heroku app's name to be. i.e. I choose the name sneakersearchinstanceAtoZ
-After you run this command if you login into Heroku and you should see the app's name listed. Go to the right corner, click the 9 dots, choose Dashboard
9.directions:
9. run: heroku create sneakersearchinstanceAtoZ
10•heroku addons:add bonsai (install bonsai)
10.comments:
-You will need to add a credit card to your Heroku account before you run this step otherwise after you run it it will say you have to do it. It's still a free plan though. I'm not sure if you can add a Bonsai add on without a card
-To add a credit card go all the way to the right side, click the circular icon, choose Account Settings, then choose Billing
-You now need to add bonsai to your heroku app. You do this by running heroku addons:add bonsai --app <your-app-name>
-<your-app-name> is whatever the heroku app's name you created in step 9
-after you create a bonsai cluster it will then want you to open up the cluster by running heroku addons:open bonsai
10.directions:
10A. log on to Heroku and add a credit card to the Billing Page
10B. run: heroku addons:add bonsai --app sneakersearchinstanceAtoZ//same exact name from step 9
10C. run heroku addons:open bonsai
11•heroku config (check bonsai instance info and copy your new
BONSAI_URL you will need it later)
11.comments:
-Running heroku config will get all your environment variables and their corresponding values. When you see your bonsai's url, copy it's value, you'll need it for steps 12B and 17
-This is the exact same url you will see from step 10C
-The bonsai url might look something like https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net
11.directions:
11A. run: heroku config
11B. Copy the bonsai url that is returned
12•heroku config:set FB_NAME= FB_TOKEN="" (declare
environment variables)
12.comments:
-12A is REQUIRED. You need to use your project's GoogleService-Info.plist's PROJECT_ID which is the FB_NAME and the API_KEY which is the FB_TOKEN for this step
-12B is optional. If you want to connect to ElasticSearch locally run this. You will have to set your bonsai url so that running node app.js won't throw connection errors. You do this by running: export BONSAI_URL="<your_bonsai_url>". You will need to run this command per terminal session or if your Heroku app falls asleep/crashes.
-Do not use spaces before or after the equal sign
12.directions:
12A(REQUIRED). run: heroku config:set FB_NAME=sneakersearch-az12 FB_TOKEN=0012abc789xyz00019
12B(Optional).run: export BONSAI_URL="https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net"
13•git add config.js (update)
13.comments:
-If you run this you might get an error that says:
The following paths are ignored by one of your .gitignore files:
config.js
Use -f if you really want to add them.
-It's saying to run git add config.js then add -f at the end
13.directions:
13A. run: git add config.js
13B. If there is an error run: git add config.js -f
14•git commit -m "configure bonsai"
14.directions:
14. run: git commit -m "configure bonsai"
15•git push heroku master (deploy to heroku)
15.directions:
15. run: git push heroku master
16•heroku ps:scale worker=1 (start dyno worker)
16.comments:
-the answer to what this command does is here Can someone explain "heroku ps:scale web=1"
16.directions:
16. run: heroku ps:scale worker=1
17•curl -X POST /firebase (ex:
https://username:password#yourbonsai.bonsai.io/firebase)
17.comments:
-Before you begin this step read #DoesData updated answer (below mines) to use curl -X PUT instead of POST
-Grab the bonsai url you copied from step 11B
-Paste the url after the curl -X POST command and make sure you add /firebase to the end of it
-what this does is it creates an index named /firebase that points to your bonsai url. Open your config.js file and look for exports.paths (line 62). That has a key/value pair named index: "firebase"(line 65) , the value is pointing to the index you just created and accesses the ES cluster from there
-Basically in your config.js file, on line 65 the index value firebase has to exactly match the name /firebase that your adding to the end curl -X POST command. If the names don't match then none of this will work.
-if successful you should get this response back: {"acknowledged":true}
17.directions:
17. run: curl -X POST https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net/firebase
18•Now time to use Step 5 node app.js (run the app)
18.comments:
-running this command will start the app locally. You can also run npm start but either way it will launch it on your local machine and you will need to run the code in Step 12B(Optional) for this to work
18.directions
18. run: node app.js
19• set your mappings object(s)
FYI Step 19 is very important. You MUST set your MAPPINGS OBJECT and it's KEYS. I don't have enough chars left to explain it in detail here. This is what Step 4's type key uses to get set by. Watch this video to explain how to set your mappings object: https://www.youtube.com/watch?v=h3i3pqwjtjA&feature=youtu.be
19.directions:
19A. --open TextEdit or Sublime then create a file and name it sneakerfile.json. Save it and drag the file into your flashlight folder. You need the .json extension.
19B. -Inside that file add the following code, save and close the file:
{
"sneakers": {
"properties": {
"sneakercondition": {
"type": "string"
},
"sneakername": {
"type": "string"
}
}
}
}
//notice this is just like the Swift Sneakers Data Model declared at the beginning
19C. grab your BONSAI_URL run heroku config:get BONSAI_URL. My BONSAI_URL is https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net
19D. run: curl -XPOST https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net/firebase/sneakers/_mapping -d#sneakerfile.json
19E. run: curl -XGET <BONASI_URL>/firebase/sneakers/_mapping?pretty
Done with Part 1. Part 2 will explain Step 19 in detail
Further Info-
Heroku commands:
To check on your dyno
run: heroku ps
If you get the following then refer to the links below
=== 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)
After creating your Heroku app your dyno is under the Heroku Free Plan. It will eventually fall asleep/crash if it's not being used within a certain time frame. To stop sleeping/crashing follow
Easy way to prevent Heroku idling?
https://devcenter.heroku.com/articles/free-dyno-hours
https://devcenter.heroku.com/articles/dyno-sleeping
Other commands:
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
To get back to the command prompt press Ctrl+C
You can also contact Heroku Support or Bonsai Support as they are very helpful
I will post Part 2 very soon.
The above answer is great. However, there are far too many comments for my update to be useful there so I'm placing it here.
If you follow the above steps you will encounter an error on step:
run: curl -X POST https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net/firebase
Newer versions of elasticSearch no longer allow you to create a new index using POST, but you can use PUT. So you want to run
curl -X PUT https://abc123a01:01abc12de45xyz34#xyz-012345.us-east-1.bonsaisearch.net/firebase
and the response should be: {"acknowledged":true,"shards_acknowledged":true}
See this question for more information.
Update
I also switched over to Algolia because the documentation was a lot more clear. However, I would say that the elastic search documentation is in drastic need of an update. I would recommend that those interested in elastic search try to deploy their code using Firebase cloud functions rather than Heroku. I haven't been able to find a guide for this, but the node.js code should be similar except it will be deployed to the Firebase cloud instead of Heroku. This should make the process a lot simpler. You can check out Firebase cloud functions here

How can I set NODE_ENV variable for ironworker (iron.io) task?

Is there a way to set/configure NODE_ENV for an ironworker task? It's not set in the process.env output. Because of that the config module loads config/development.json configuration file.
There are a couple ways to set this value. First you can send ENV params with the -e flag, as noted here https://github.com/iron-io/dockerworker/tree/master/node#2-test-locally
Iron actually recommends that you provide this information as part of the configuration and payload when creating a task.
The way Iron recommends doing this is a bit annoying (http://dev.iron.io/worker/reference/configuration-variables/#config-via-file):
Upload a config.json/yml file by specifying --config-file when uploading the code via the iron cli
Read the path to this config file via the CONFIG_FILE environment variable inside the container
Read the contents of this file, and parse them as JSON
I have another method which is easier: using arguments to npm start:
In your Node code, read the environment via env = process.argv[2] || process.env.NODE_ENV
Pass npm start -- <env> as the run command to the iron cli

How to use a private npm registry on Elastic Beanstalk?

We have a nodejs project running on Amazon Elastic Beanstalk that uses private modules that we host using nodejitsu's private npm registry.
However getting access to the private npm registry from the elastic instances hasn't been straightforward and is not documented well.
What is the best way to get this access set up?
None of the other answers were working for me. After hours of hair pulling, we finally figured it out. The solution that worked is almost the same as the other answers but with a very minor tweak.
Set an NPM_TOKEN environment variable on Elastic Beanstalk under Configuration > Software Configuration > Environment Properties.
Create a .ebextensions/npm.config file. (The name does not have to be 'npm'.)
Put this content into the file:
files:
"/tmp/.npmrc":
content: |
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Note that it uses ${NPM_TOKEN} and not $NPM_TOKEN. This is vital. Using $NPM_TOKEN will not work; it must have the curly braces: ${NPM_TOKEN}.
Why are the curly braces needed? No idea. In shell/POSIX languages, ${VAR} and $VAR are synonymous. However, in .npmrc files (at the time of this writing), variables without the curly brackets are not recognized as variables, so npm must be using a slightly different syntax standard.
UPDATE
Also, this has worked for us only on new or cloned environments. For whatever reason, environments which were not initialized with a /tmp/.npmrc will not read it in any future deployments before running npm install --production. We've tried countless methods on 4 different apps, but cloning and replacing an environment has been the only method which has worked.
So, we managed to get this working by using the npm userconfig file. See the doc page for npmrc for more info.
When a nodejs application is being deployed to Elastic Beanstalk, the root user runs npm install. So you will need to write the root's npm userconfig file, which is at /tmp/.npmrc.
So if you add a file called private_npm.config (or whatever name you choose) to your .ebextensions folder with all the information needed, you will be good to go. See Customizing and Configuring AWS Elastic Beanstalk Environments for more info.
So here is what my file looks like to use nodejitsu private registry.
.ebextensions/private_npm.config:
files:
#this is the npm user config file path
"/tmp/.npmrc":
mode: "000777"
owner: root
group: root
content: |
_auth = <MY_AUTH_KEY>
always-auth = true
registry = <PATH_TO_MY_REGISTRY>
strict-ssl = true
email = <NPM_USER_EMAIL>
Using an .npmrc within the project also works. For example...
.npmrc
registry=https://npm.mydomain.com
You may want to .gitignore this file if you include an _authToken line but make sure you don't .ebignore it so it's correctly bundled up with each deployment. After trying a few things unsuccessfully, I came across this post which made me realize specifying it locally in a project is possible.
The answer above as a step in the right direction, but the permissions and owner did not work for me. Managed to get it to work with the following combination:
files:
#this is the npm user config file path
"/tmp/.npmrc":
mode: "000600"
owner: nodejs
group: nodejs
content: |
_auth = <MY_AUTH_KEY>
always-auth = true
registry = <PATH_TO_MY_REGISTRY>
strict-ssl = true
email = <NPM_USER_EMAIL>
Place the below within your .ebextensions/app.config.
files:
"/tmp/.npmrc":
mode: "000777"
owner: root
group: root
content: |
//registry.npmjs.org/:_authToken=$NPM_TOKEN
Where NPM_TOKEN is an environment variable with the value of your actual npmjs auth token.
Note that environment variables within elasticbeanstalk can and should be set from within the AWS console Elasticbeanstalk software configuration tab.
AWS Elasticbeanstalk Configuration
In new Elastic Beanstalk Linux 2 Platforms, none of these solutions work (apart from the .npmrc file solution that works but has its issues when using them in development evironments due to the requirements that all developers have their ${NPM_TOKEN} Env Var defined in their own environments).
The reason is that the /tmp/.npmrc location no longer works.
Option 1
You have to change the .ebextensions/npm.config file to this new format:
files:
#this is the npm user config file path
"/root/.npmrc":
mode: "000777"
owner: root
group: root
content: |
_auth= ${NPM_TOKEN}
registry = https://{yourprivatenpmrepository.com}/
Option 2
Add a custom .npmrc_{any-suffix} to the root of your app and create a prebuild hook to rename it before Beanstalk executes the npm install so that it can use your private repository configuration:
Add the following file (path from your app root) .platform/hooks/prebuild/01_set_npmrc.sh with the following content:
#!/bin/bash
#Copy and rename .npmrc_beanstalk to .npmrc
mv .npmrc_beanstalk .npmrc
Create an .npmrc_beanstalk file in your root with the following content (modify it depending on your private npm config):
_auth= ${NPM_TOKEN}
registry = https://{yourprivatenpmrepository.com}/
Chmod the hook file so that it has the necessary exec permissions when uploaded to EB: chmod +x .platform/hooks/prebuild/01_set_npmrc.sh
Re-deploy using EB CLI and you are done!
With modern platforms, you no longer need to do this via .ebextensions
You can simply create a .npmrc file at the root of your deployment package, alongside your package.json with the following line:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Using this method, you can create an environment variable named NPM_TOKEN in your AWS console so you don't have to store the token in your repo.
Structure:
~/your-app/
|-- package.json
|-- .npmrc

Resources