Deploying to Heroku
$ npm install -g heroku
$ heroku create
Created: https://lit-crag-58882.herokuapp.com/ | https://git.heroku.com/lit-crag-58882.git
$ heroku open -- opened newly created heroku app in browser
$ git remote -v -- now we have origin and heroku
$ heroku create -a face-reco-backend-86 -- I decided to create app with custom name
$ git remote -v -- But here we still have lit-crag-58882, not the newly created app
$ heroku apps:destroy --app lit-crag-58882 --confirm lit-crag-58882
$ git remote add heroku https://git.heroku.com/face-reco-backend-86.git
We deleted not needed app and added new app instead
$ git push heroku main
$ heroku open -- error - let's see logs
$ heroku logs --tail
Error "sh: 1: nodemon: not found"
Solution: modified package.json:
"scripts": { "start": "node server.js", "start:dev": "nodemon server.js" }
We continue using nodemon in local development environment, but not during production on the Heroku server.
$ git add .
$ git commit -m "scripts in package.json modified to run nodemon only in local dev environment"
push to github and heroku, and open:
blank page and hanging loading...
Logs: "Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch"
Solution: modify server.js:
app.listen(process.env.PORT || 3000, () => ...)
Add, commit, push and open:
It starts, goes with route
app.get('/', (req, res) => {...})
Receives respond with message and shows it, but in logs we see error "connect ECONNREFUSED 127.0.0.1:5432", because it was a local connection to our database.
Connecting to Postgres on Heroku
-
On Heroku add Postgres to our App
-
Connect to db
https://devcenter.heroku.com/articles/heroku-postgresql
https://devcenter.heroku.com/articles/managing-heroku-postgres-using-cliheroku addons
to check if DB is already connected (in our case yes)
heroku pg:info
some info about connected database
heroku pg:psql
connecting to our "postgresql-concave-33478" -
Create tables
Go to pgAdmin and copy queries from historyCREATE TABLE login ( id serial PRIMARY KEY, hash varchar(100) NOT NULL, email text UNIQUE NOT NULL );
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100), email TEXT UNIQUE NOT NULL, entries BIGINT DEFAULT 0, joined TIMESTAMP NOT NULL );
\d
check created tables
\q
quit psql session -
Modify connection details inside our server.js
https://devcenter.heroku.com/articles/connecting-heroku-postgres#connecting-in-node-jsconnection: { connectionString: process.env.DATABASE_URL, ssl: { rejectUnauthorized: false } }
-
Register new user
Add, commit, push.
Go to front page and register new user -> successfull, POST request can be seen in heroku logs.