Node Js deploy on Heroku

I’ve been trying to learn server side programming using node js. I’m running node js on localHost and now want to deploy some code that will run so that it can be accessed outside of my PC. I’ve been following the Code Train videos and the one below shows how to do some web application deployment.

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. To use it on your PC you need to download theCLI (Command Line Interface ) so you can run it from your Terminal, in my case, from within VS Code.

I’ve created a Heroku account and have logged in. I’m offered 2 locations to host the app, either the US or Europe. I’m going to choose US.

So for this I’m taking the code from CodeTrain for the Selfie App and downloading onto my computer.

Next I’m backing up to Github so I have a github repository (then I’ll be able to use the git commands for this app).

I have changed the port issue code as per his video above , so that it works on Heroku and localHost.

const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening at port ${port}`));

I went through the process as per the video, first off doing :

>heroku login

>git push heroku master

This pushed (after git add . then git commit -m”blah”)

Heroku built the app but there was an error. It tells you to go and look at the logs. The log said that the package.json file did not have a start script, so I looked on the app Daniel deployed (this selfie one is Module 2 & the one he deployed is module 3 weather app). I looked in that package.json file and it had:

“start”: “node index.js”,

Which was not in my package.json file so I added the code to the file and saved, then did git add “package.json”, git commit and git push heroku master and it went and built itself again and runs. So tested it out at:

https://self-app-locate.herokuapp.com/

As the heroku is a https:// site we can access it from the mobile device.

Soit asks you for permission to use web cam and location. There is a field to fill in with a “mood”, then you press enter, it takes a picture with thr cam and gives you the IP location in Lat /Long. There is another tab that lists the entries in the nedb database. See image below:

Heroku access

I’ve the app up and running on Heroku and there is a loghtweight database nedb attached to it. I took some images which were sent to the database and displayed back to the phone. That was great, then I thought I’d like to access that database file. I had a look on the Heroku website and can’t figure out how to do that. If that is the case then the idea works, good envirnment to test, but not to deploy if you have no access to the data later.

I need to do a bit more research on that. I womnder if I can pull from Heroku back to my folder or to github.

There is a console command once you are in app in Heroku:

then type >cat database.db

This will show data in console as below , but you can’t cut/paste data.

Heroku databases and add-ons

There are databases on Heroku that you can use, you have to give credit card details but you can get free instances up to 5MB to use. So instead of putting data in a file on Heroku you can link to the Database and then access this database from a MySQL /PHP Panel to interogate the database. See video.

Heroku/github

Now that I’ve had a first play with Heroku and have an app running I need to explore it a bit more. One thing that seems to be interesting is that you can link back to Github directly instead of from your PC.

I’m thinking about a scraping app that gets the data from the website at regular intervals and updates a JSON file (So a simple API) hosted on Github that will update, andf be able to be called with the https://raw.gig…Blah.json so it can be consumed either server or clientside on a webpage.

this is an interesting video on continuous deployment from Github to Heroku, but only from Github to Heroku.

End comment

When I was initially watching the videos and looking at coding the app I was excited that you could get locations from a mobile phone. Then the coding was done on local server so you just got the same location, I was a little dissapointed.

Then at the end with Deploying Node.js I saw there was an oppertunity to run this on a server that was accesssible from the internet such that it is able to be used on a mobile device so could Geolocate, I’ve yet to confirm this as its raining heavily outside so I cant test it.

I’d got a You Tube question from someone asking about glideapps and using it to Geolocate low risk ex prisoners via a phone app to see that they were staying within the bounds of their parole requirements. This would meet that need, with a bit of modification to the code.

I took it