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

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!

Related

Problems Deploying Node App With Render.com

I would like to post a simple stripe integration on render. I apologize in advance for my ignorance on the topic but, since I'm more front-end oriented, it's the first time I've attempted to do such a thing so I would like to ask you
if the repository https://github.com/Luca-Liseros-Ferrari/stripe-example.git is ready to be published, if there is some error that can cause the deploy to fail or if some preliminary operation is required (in the server folder I also have an .env file with the stripe keys and I specified STATIC_DIR = "../client/")
In render.com after clicking on "new" - "web service" and connecting the github repository and considering that from the terminal I start the server.js with the following commands:
cd server
node server.js
how should I fill in the "root directory", "build command" and "start command" fields since it's still not clear to me? Is the root directory the folder that contains the server.js file inside? In my case it would be for example "folderName/server" or simply "server"?
I tried to upload the repository to render but i get the following error message
Failed - Exited with status 1 while running your code.
It also tells me "error cannot find module express"
then I reinstalled express in server folder with npm install express and verified it was already installed. I therefore believe that there is a path error in the phase in which I create the web service.
error snippet
I hope I have provided enough data and I thank anyone who is willing to give me a hand in advance
I solved the problem. I had to specify in render.com in advanced the key - value pairs of my .env file
I noticed it thanks to the Cyclic app which, after loading the repository, warned me that if the app doesn't work it could be because of that
I hope it will help someone

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

Implementing authentication with passport / node.js and getting error

I had already implemented authentication for the app I'm working on but am trying to refactor it based on a recent tutorial I did which I thought was very clear and also involved adding facebook / google / twitter auth which I would like to do.
So far, I've updated the user model and defined my local strategy for signup but when I go to try and run the app now, I'm getting an error. I have the app uploaded to github and wondered if somebody would be able to check it out and see where I'm going wrong. The error is pointing to a part of the index file until node_modules / express but I cannot work out what's up and I'd like to know before I proceed further.
Latest commit is under: https://github.com/DaveBage83/friendly-invention
Thanks!
A few things for you notice.
1 - Do not commit the node_modules folder. Once you have all your dependencies in the package.json file, the npm install will download all of for you again.
2 - This code is full of erros. I won't put everything here, I believe you find them by yourself. Otherwise, put the specific code here, so we can help you out.
About the question:
In you app.js file.
...
authRoutes = require('./routes/index')(app, passport)
...
If you look at the ./routes/index, you'll see that it is exporting a route object, witch does not expect the two parameters. (e.g. (app, passport)).
Hope it can still help you.

ExpressJS, NodeJs, and MongoDB CRUD Skeleton - Mac

I have recently started working with MongoDB and had it working for a small web test with node JS However. I new my implementation wasn't following a decent MVC structure. I began searching and found this website and read through it. It's implementation look good as well as following a good MVS skeleton structure.
Express/NodeJS/MongoDB CRUD Skeleton
I got to the part where he says 'HOORAY! We've got a functioning web server that is talking to Mongo. Part 1 is accomplished." Unfortunately, I went the local host specified and it says the 'site cannot be reached'. I am still fairly new to NodeJs, Mongo and Express and cannot figure out why it is not working.
I am not sure if I have done a step wrong time and time again which I doubt or if there something is missing that is stopping me from having it working.
The part before it says:
Awesome. Now to test it's all working in the terminal type npm start and you shouldn't see any errors:
kcoleman-mbp:nodewebapp kcoleman$ npm start
> nodewebapp#0.0.1 start /Users/kcoleman/Documents/projects/nodewebapp
> node ./bin/www
That part I get the same outcome with:
nodewebApp and node./bin/www
The page says to navigate to 127.0.0.0:3000, but the localhost usualy defaults to 127.0.0.1. I didn't see any code in the page where he defines the port to 127.0.0.0.
So try connecting to 127.0.0.1:3000.
Coming to nodewebApp and node./bin/www, it could be because the command might be present in pacakge.json. If you delete the nodewebApp from package.json, you won't be seeing the same in the command.

nodejs/redis auth works locally (w/Foreman), but not on Heroku

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.

Resources