In this project, you'll see a burger logger with MySQL, Node, Express, Handlebars and a homemade ORM (yum!).
-
Eat-Da-Burger! is a restaurant app that lets users input the names of burgers they'd like to eat.
-
Whenever a user submits a burger's name, your app will display the burger on the left side of the page -- waiting to be devoured.
-
Each burger in the waiting area also has a
Devour it!
button. When the user clicks it, the burger will move to the right side of the page. -
This app will store every burger in a database, whether devoured or not.
NPM packages used for this project:
Node APM | Description |
---|---|
express | https://www.npmjs.com/package/express |
express-handelebars | https://www.npmjs.com/package/express-handlebars |
mysql | https://www.npmjs.com/package/mysql |
- In addition, you need to add this add-on to your heroku:
- JawsDB MySQL
$ heroku addons:create jawsdb
$ heroku config:get JAWSDB_URL
$ heroku config -s | grep JAWSDB_URL >> .env
$ more .env
-
Create a GitHub repo called
burger
and clone it to your computer. -
Make a package.json file by running
npm init
from the command line. -
Install the Express npm package:
npm install express
. -
Create a server.js file.
-
Install the Handlebars npm package:
npm install express-handlebars
. -
Install MySQL npm package:
npm install mysql
. -
Require the following npm packages inside of the server.js file:
- express
-
Inside your
burger
directory, create a folder nameddb
. -
In the
db
folder, create a file namedschema.sql
. Write SQL queries this file that do the following:- Create the
burgers_db
. - Switch to or use the
burgers_db
. - Create a
burgers
table with these fields:- id: an auto incrementing int that serves as the primary key.
- burger_name: a string.
- devoured: a boolean.
- Create the
-
Still in the
db
folder, create aseeds.sql
file. In this file, write insert queries to populate theburgers
table with at least three entries. -
Run the
schema.sql
andseeds.sql
files into the mysql server from the command line -
Now you're going to run these SQL files.
-
Make sure you're in the
db
folder of your app. -
Start MySQL command line tool and login:
mysql -u root -p
. -
With the
mysql>
command line tool running, enter the commandsource schema.sql
. This will run your schema file and all of the queries in it -- in other words, you'll be creating your database. -
Now insert the entries you defined in
seeds.sql
by running the file:source seeds.sql
. -
Close out of the MySQL command line tool:
exit
.
-
-
In order to deploy this project on Heroku, you will need to create this database in JawsDB as well. You need to make sure to use your host, username and password from https://dashboard.jawsdb.com/mysql/
-
Inside your
burger
directory, create a folder namedconfig
. -
Create a
connection.js
file insideconfig
directory.-
Inside the
connection.js
file, setup the code to connect Node to MySQL. -
Export the connection.
-
-
Create an
orm.js
file insideconfig
directory.-
Import (require)
connection.js
intoorm.js
-
In the
orm.js
file, create the methods that will execute the necessary MySQL commands in the controllers. These are the methods you will need to use in order to retrieve and store data in your database.selectAll()
insertOne()
updateOne()
-
Export the ORM object in
module.exports
.
-
-
Inside your
burger
directory, create a folder namedmodels
.-
In
models
, make aburger.js
file.-
Inside
burger.js
, importorm.js
intoburger.js
-
Also inside
burger.js
, create the code that will call the ORM functions using burger specific input for the ORM. -
Export at the end of the
burger.js
file.
-
-
-
Inside your
burger
directory, create a folder namedcontrollers
. -
In
controllers
, create theburgers_controller.js
file. -
Inside the
burgers_controller.js
file, import the following:- Express
burger.js
-
Create the
router
for the app, and export therouter
at the end of your file.
-
Inside your
burger
directory, create a folder namedviews
.-
Create the
index.handlebars
file insideviews
directory. -
Create the
layouts
directory insideviews
directory.-
Create the
main.handlebars
file insidelayouts
directory. -
Setup the
main.handlebars
file so it's able to be used by Handlebars. -
Setup the
index.handlebars
to have the template that Handlebars can render onto. -
Create a button in
index.handlebars
that will submit the user input into the database.
-
-
All the recommended files and directories from the steps above should look like the following structure:
.
├── config
│ ├── connection.js
│ └── orm.js
│
├── controllers
│ └── burgercontroller.js
│
├── db
│ ├── schema.sql
│ └── seeds.sql
│
├── models
│ └── burger.js
│
├── node_modules
│
├── package.json
│
├── public
│ └── assets
│ ├── css
│ │ └── style.css
│ └── img
│ └── burger.jpg
│
│
├── server.js
│
└── views
├── index.handlebars
└── layouts
└── main.handlebars
Have fun!