nodejs/redis auth works locally (w/Foreman), but not on Heroku - node.js

It looks like my req.session (.user) doesn't exist when my node app is on Heroku. Things work on my local machine, and when I push to heroku, I have access to my redis-to-go store for other uses, but when I log in and proceed to authenticated pages, I get "Internal Server Error" on the served page, and "Cannot read property 'currentUser' of undefined" in the log, w/undefined = req.session, I believe.
Also, I have the same redistogo code at the start of both files:
if process.env.REDISTOGO_URL
rtg = require("url").parse(process.env.REDISTOGO_URL)
redis = require("redis").createClient(rtg.port, rtg.hostname)
redis.auth(rtg.auth.split(":")[1])
else
redis = require("redis").createClient()
edit:
I'm still rather new to Heroku/express/redis, and I realized that the problem may be that the code above isn't in the function that's being exported and called, so I moved it in, but I can't get it to deploy. After
git add .
git push heroku master
heroku releases
I get the same old list of deploys and rollbacks that I had before…
so, 1) My problem is that I had my code outside of the exported fn??
2) How do I push my changes? Have I possibly run out of memory on Heroku? my program isn't very large…
Edit2 - I figured it out. There were several small changes I needed to make.
Thanks,
John

Try changing your first line to:
if (typeof(process.env.REDISTOGO_URL) !== 'undefined')
If that doesn't work, add console.log statements to see which side of the if..else statement you're hitting on Heroku.

Related

getting 401 response codes on a deployed React App that worked fine in development?

I am trying to complete a Redux project and got everything to work fine in development on my own machine but when deployed, the browser is failing to recognise my hidden environment variables so not passing client keys to Api calls and thus making unauthorised requests. I have a '.env' file with the keys in them and then refer thus...
const API_URL = https://api.openweathermap.org/data/2.5/weather?appid=${process.env.REACT_APP_OPEN_WEATHER_MAP_API_KEY}&units=metric;
I have looked up the problem on here and think I understand what's going wrong but don't know how to put it right. I am deploying site via Netlify from my Github repo and my build settings appear to be correct. I thought 'npm run build' took care of all that for me?
99% finished a project and now scratching my head furiously...
Try defining variables in .env file starting with REACT_APP like:
REACT_APP_CLIENT_ID=4
REACT_APP_API_URL=/api
Try using it anywhere like:
const apiUrl: process.env.REACT_APP_API_URL;
Set the environment variables specifically within the Netlify UI. So 'Deploy Settings > Environment > Environment Variables' then variable -'REACT_APP....' value - '*****' for each variable you want defined.

How do I get my deployed React.js app to use API secret keys? I'm using Heroku confing vars but still not working

Problem:
I created a simple React app (using Javascript and Node) that uses the GitHub API to search for users and return information about them. I need to use a GitHub oauth key so that I can make authenticated API requests. However, I am having trouble giving my deployed app (using Heroku) the key without hard-coding it into the API call. I'm fairly new at this so any help would be great! I linked the github repo at the bottom of this post.
I have tried several things which I will explain below:
Attempt 1:
I created a file where I set my GitHub key to a variable and exported it (Image of code)
I put said file in the .gitignore
I imported the variable in the files where I made API calls and used them it directly in the API call. (Image of API call)
This worked on my dev environment but (obviously) did not work on my deployed Heroku app because it had no idea what the variable was. (Image of error)
Attempt 2:
I configured variables in Heroku and set GITHUB_KEY to my key. (Image of Heroku variable setting).
Next, I checked that Heroku recognized this variable by running the command heroku config:get GITHUB_KEY and received the correct key in response (Image of terminal)
In my secrets file, I set the variable like so: process.env.GITHUB_KEY = 'a93b2c21918b42df5a28e0e529c627ee22c60de4'; (Image of setting variable using process.env)
And then I use it in my API calls on the frontend: const res = await fetch(
'https://api.github.com/users/${this.state.input}?access_token=${process.env.GITHUB_KEY}'
);.
However, I get the following error: SearchBar.js:32 GET https://api.github.com/users/livmarx?access_token=undefined 401 (Unauthorized). (Image of error).
So, I know that I'm misunderstanding how process.env works but cannot seem to figure it out! Any clarification would be super helpful.
Here is the link to my github repo: https://github.com/livmarx/zilliow-challenge

Following along with the Heroku docs to create a MEAN stack app and I am stuck

I am following along with this doc here:
https://devcenter.heroku.com/articles/mean-apps-restful-api
And I am stuck on the test curl that is supposed to create a new "contact" in the Mongo database.
Up until that point I have followed the directions to the letter. The only difference is that I cloned an existing Heroku app locally instead of creating one in the CLI. I don't think this is causing an error. I cloned the blank app, installed node and dependencies using npm, then created the server.js file exactly like they said and pushed everything to my app.
It seemed successful, I can see in my Heroku account that my previously blank app is now a node app, and I can visit my attached Mongo database. However, I don't see any collections in my database, and this test curl fails:
curl -H "Content-Type: application/json" -d '{"firstName":"Chris", "lastName": "Chang", "email": "support#mlab.com"}' http://your-app-name.herokuapp.com/contacts
The contact is not posted to the database like it should be. I get a really weird response, the HTML for an application error shows up in my command prompt window, like I visited the wrong page in my app. I don't get any sort of error from express.
I am completely new when it comes to anything MEAN stack and Heroku. I did some courses in code school for node and express, but that's it. Anyone have this type of error when trying to set up MEAN on Heroku? The code that server.js should include is in the article I linked.
Edit
I figures this one out. I didn't have anything in my routes.
Figured this out myself. My routes weren't set up correctly. Watch those routes!

Heroku Review App config and logging (vs. production config/logging)

I'm looking into some issues I'm having with my Heroku Review Apps.
It appears that from Bash, there's no way to access the config (environment variables) or logging. If I run:
heroku logs --tail
This just comes back with logs from the production app (which makes sense, since there doesn't seem to be a way to keep the scope at the review app). I can see the Review App logs from within the Heroku dashboard, but even this seems a bit off, as subsequent refreshes will omit different lines from the log (which is why I'm trying to just see them from Bash).
Also, if I do:
heroku config
It returns back the expected config values (i.e. DATABASE_URL), but again, I'm not sure if this is just for production.
For example, running this from the review app:
console.log("process.env.PORT: " + process.env.PORT);
console.log("process.env.DATABASE_URL: " + process.env.DATABASE_URL);
console.log("process.env.PAPERTRAIL_API_TOKEN: " + process.env.PAPERTRAIL_API_TOKEN);
The PORT is returning a value, but both DATABASE_URL AND PAPERTRAIL_API_TOKEN are coming back undefined, even though these are both populated in heroku config.
So my questions are:
1) How do I view a Review App's logs from Bash (or any other reliable method other than the Heroku dashboard)?
2) Are the config values different between Review Apps and production, and if so, how do I configure them for Review Apps?
Use --app to point to your review app, e.g:
heroku config --app my_review_app
heroku config:set VAR=VALUE --app my_review_app
Yoni is exactly right, but here are more details.
When running heroku config, the first line of the response shows the app it is currently pointing to. This should've been my first clue, and I missed it initially.
$ heroku config
=== fathomless-meadow-38193 Config Vars
DATABASE_URL: postgres://xxxxxxxxxx...
Once I ran the config for my review app, it indeed returned empty.
$ heroku config --app blooming-river-14132-pr-2
=== blooming-river-14132-pr-2 Config Vars
As Yoni said, you can use the -app switch to specify an app when setting config variables. But I wanted my default app to point to my blooming-river app, which lead me to this post on how to change this:
$ heroku git:remote -a blooming-river-14132-pr-2
Now I can set DATABASE_URL using:
$ heroku config:set DATABASE_URL=POSTGRES://xxxxxxxx...
I don't really like having to manually set it for each new pull request, so I found out that Review Apps can inherit config vars from a parent app. Unfortunately, I never could get this work, at least not for DATABASE_URL.
So I'm still trying to figure that out, and will update this answer if I do.
There are addons to collect the logs and have so many other features like alerts.
So you can consider using one of them. I am just giving one that actually we are using in our app called Logentries
https://elements.heroku.com/addons/logentries

Err_connection_refused using meteor uploads

I am deploying a meteor application to a digital ocean droplet with meteor upload. Everything goes well, the application gets deployed, database works, seeding of data works etc. But there is one problem i can't seem to be able to solve.
I use the meteor-uploads package (https://github.com/tomitrescak/meteor-uploads) for file uploads. Locally everything goes well, the file gets uploaded, finished callback gets called etc. But once I have deployed the application to the server it keeps giving me on of these errors, :
POST http://*ip*/upload net::ERR_CONNECTION_REFUSED
POST http://*ip*/upload net::ERR_EMPTY_RESPONSE
POST http://*ip*/upload net::ERR_CONNECTION_RESET
Any ideas are welcome, I have searched all over for a solution but none seems to fit my problem. I also installed to a fresh droplet but that didn't help. In none of my browsers (Mac Chrome, safari & firefox) does it work, on my phone (Android 5.0) I get the same errors. I am using the newest Meteor version 1.1.0.1
On local host you don't need to set the environmental variables, but the host services provides you should.
Check this tutorial to see how to put the environment variables.
Because the file-upload needs a startup-server-configuration, like this.
//file:/server/init.js
Meteor.startup(function () {
UploadServer.init({
tmpDir: process.env.PWD + '/.uploads/tmp',
uploadDir: process.env.PWD + '/.uploads/',
checkCreateDirectories: true //create the directories for you
})
});
But im not sure if putting this on a startup will work on digital ocean, like i say you you enter it, run printing and check if the /.uploads/ exists

Resources