This is a guide for myself to document my process, so I won't forget and won't get confused
npm init
git init
- install extension for vscode:
prettier
&eslint
- install eslint with:
npm install eslint --save-dev
- configure eslint with:
npm init @eslint/config
- google how to configure prettier with eslint airbnb:
https://gist.github.com/bradtraversy/aab26d1e8983d9f8d79be1a9ca894ab4
- install neccesities for eslint and prettier:
npm i -D eslint prettier eslint-plugin-prettier eslint-config-prettier
- create a
.prettierrc
file for prettier rules - create a
.gitignore
with template from:https://github.com/github/gitignore/blob/main/Node.gitignore
- create a
.env
file for future passwords and keys
-
turn on
format on save
in vscode -
choose
prettier
as default formatter in vscode -
in
.eslintrc.json
added inextends
:"prettier"
-
in
.eslintrc.json
addedplugins
:"prettier"
-
in
.eslintrc.json
added inrules
:"prettier/prettier": "error"
&"no-console": "off"
-
install express with:
npm install express --save
-
add a
server.js
file and add the code in it fromhttps://www.npmjs.com/package/express
to use express -
install nodemon with:
npm install --save-dev nodemon
-
in
package.json
add inscripts
:"dev": "npm run nodemon"
-
make a
static
folder and inside acss
andimages
folder -
make a
views
folder and insidepartials
andpages
folder -
install ejs with:
npm install ejs --save
-
google how to configure ejs:
https://www.digitalocean.com/community/tutorials/how-to-use-ejs-to-template-your-node-application
-
configure ejs in
server.js
with:app.set('view engine', 'ejs');
-
set middleware for express in
server.js
with:app.use(express.static("static"));
-
install
dotenv
with:npm install dotenv --save
-
require
dotenv
inserver.js
with:require('dotenv').config()
-
installed
ejs language support
extension for my vscode, otherwise the colors won't be blue and it confuses me -
added
partials
andpages
-
in
mongodb
I made a new project calleddish-exchange
and created a shared (free) database -
i created a user with a password so I can read and write into my database
-
I first my own IP then in
network access
I deleted my ip and clicked onadd IP adress
followed byallow access from anywhere
-
I went to
database
>browse collections
then Icreated a database
with the namedish-exchange
-
I made a collection called
dishes
-
I clcicked on
insert document
and made 2 example dishes ordocuments
in my dishes database -
started to read the mongodb documentation with node driver on atlas:
https://www.mongodb.com/docs/drivers/node/current/
- installed mongodb with:
npm install mongodb --save
- put in
server.js
:const MongoClient = require("mongodb");
- googled on how to use a
dotenv
for my link with a password for my mongodb_URI:https://northflank.com/guides/connecting-to-a-mongo-db-database-using-node-js
- in my mongodb
database
I clicked onconnect
>connect with application
- in my
server.js
I addedconst uri = process.env.mongo_uri;
instead of my password - I copied the link and put in my
password
of my user, I changedcluster0
tocluster1
(like in mongodb) and put this as a variable in mydotenv
file - I also made a
.envSample
file for later
-
I googled "Deploying with Git" and found:
https://devcenter.heroku.com/articles/git
-
One of the steps is to install Heroku CLI (linux ubuntu):
https://devcenter.heroku.com/articles/heroku-cli#install-the-heroku-cli
-
I install Heroku CLI with:
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
-
installed heroku with:
npm install heroku --save
ornpm install -g heroku --save
followed bynpm audit fix
-
create heroku remote for a new app with
heroku create -a dishexchange
-
terminal asked to press any button and to click on the link to
login
into heroku in my browser and I checked my remotes withgit remote -v
-
commit and push code with:
git push heroku main
-
went to the heroku website & to my app into:
settings
and thenreveal config vars
. I put in there my mongodb variable with password that's in my.env file
. -
because I got the
error H10
wouldn't work, I stumbled upong making aprocfile
with the code:web: node server.js
, so heroku would know what to start -
I googled to know more about procfile:
https://dev.to/lawrence_eagles/causes-of-heroku-h10-app-crashed-error-and-how-to-solve-them-3jnl
-
heroku got an
error H20
, so I googled and found:https://stackoverflow.com/questions/18679690/heroku-nodejs-app-with-r10-h10-and-h20-errors
-
instead of having:
app.listen(3000);
I changed it for heroku to:app.listen(process.env.PORT || 3000)
-
I started working on my
pages
HTML/EJS a bit, just to have the basics in there (without the web API of some sort) -
I installed the vscode extension
gitlens
as it shows extra information (e.g. commits, branches, remotes) about my project when I go into thesource control
tab.
- installed
body-parser
withnpm install body-parser --save
- added
const= bodyParser = require('body-parser')
inserver.js
-
googled "how to link variables from
server.js
toejs
" and found:https://www.geeksforgeeks.org/how-to-make-js-variables-accessible-to-ejs-files/
-
hard code variables I have in my
.ejs
intoserver.js
so I can open it from localhost:3000 -
working on connecting database googled
https://www.mongodb.com/docs/drivers/node/current/usage-examples/count/
-
with
collections.find
it will return acursor
, because if it would return all the data at once, the pc's memory might be full and the application wouldn't work good, so instead it returns acursor
. source: https://www.mongodb.com/docs/drivers/node/current/fundamentals/crud/read-operations/cursor/ -
You can retrieve data from a cursor with
toArray()
if you do want everything at once and it can handle it, or withforEach()
to look through them one by one. -
.length is an array property
-
In
server.js
I got the cursor ofdishesCollection
and got every dish withtoArray()
-
put data in server.js in route
/
-
added
numberOfDishes
andallDishes
-
In
dish.ejs
we want data fromallDishes
withforloop
- googled "how to insert a document into a collection of MongoDB with node.js driver" and found:
https://www.w3schools.com/nodejs/nodejs_mongodb_insert.asp
- created a test document that i could post into my mongodb
- it wasn't clear for me on how to use bodyparser with
https://www.npmjs.com/package/body-parser
so i googled and found:http://expressjs.com/en/resources/middleware/body-parser.html
and alsohttps://www.tutorialspoint.com/expressjs/expressjs_form_data.htm
- ended up googling how to insert document into mongodb and found:
https://www.youtube.com/watch?v=4EXR5rhcEtw
which I've watched until 5.30 - made the
add-dish
res.redirect to theinsertedId
- in
.eslintrc.json
I turned off"prefer-destructuring": "off"
, becauce I just don't want to bother understanding it now and otherwise it will make my code look confusing. - working on webAPI, thirdpartyAPI, progressive enhancement with media device API and spoonaculair
- example code: https://usefulangle.com/post/352/javascript-capture-image-from-camera
-
ADDED BTN IN ADD-DISH - ID AUTODETECTEDFOODBUTTON WITH CLASS HIDDEN
-
ADDED SCRIPT IN ADD-DISH
-
ADDED HIDDEN IN CSS FILE
-
need to - ADD SCRIPT PATH
-
ADDED VIDEO AND CANVAS
-
epreventdefault so it wont post
-
post request to spoonaculair url with fetch
-
json string is what you need to send so you want to send a (name) file , in spoonaculair it wwants a binaryimgfile
-
how to get binaryimgfile:
- try & catch error in
server.js
andadd-dish
page - edited class
.error
tocolor red
incss
try
= defines the code block to run (to try)
catch
defines a code block to handle any error
finally
or catch
= defines a code block to run regardless of the result
throw
defines a custom error
source = https://www.w3schools.com/jsref/jsref_try_catch.asp
heroku --version
= check heroku version
heroku login
= login with heroku on the browser
heroku create -a example-app
= to make a new heroku remote and an empty heroku git repo
git remote -v
= check remotes
heroku git:remote -a example-app
= to make a heroku remote for the existing example-app
git remote rename heroku heroku-staging
= rename a remote (this case heroku to heroku-staging)
git push heroku main
= to deploy code after commiting
source: https://devcenter.heroku.com/articles/git
<% %>
= to run code
<%= %>
= replace code with text
<%- %>
= to include parials
source: https://ejs.co/#docs
-
let database
= variable of the database dish-exchange -
let dishesCollection
= variable of dishes collection within dish-exchange -
cursor
=dishesCollection.find()
in route/
-
allDishes
= showing all documents indishesCollection
with:await cursor.toArray()
in route/
-
numberOfDishes
=allDishes.length
as a number shown ondishes.ejs
in route/
-
dishId
= id of the dish for a route in/dish/dishId
indish.ejs
andserver.js
-
newDish
= object with data from the form inadd-dish.ejs
, which will get posted and turned into a document inmongodb
-
urlId
= id used in the route fordish-details
page
numberOfDishes
= number of total documents in mongodb
dishIngredients
= value: ingredient, array of ingredientsdishName
= value: name of the dish indish.ejs
&dish-details.ejs
dishImage
= value: image in /images indish.ejs
dishTags
= value: tags, which is an array indish.ejs
dishQuality
= value: quality, which is a number indish.ejs
dishName
= value: name of the dish indish.ejs
&dish-details.ejs
dishImage
= value: image in /images indish.ejs
dishTags
= value: tags, which is an array indish.ejs
dishQuality
= value: quality, which is a number indish.ejs
dishId
= id of the dish for a route in/dish/dishId
indish.ejs
andserver.js
arrayNumber
= a loop that increases the number of arraynumber depending on the amount of documents saved in mongoDB dishes indish.ejs