Steps:
- Structure - create the following files/folders:
- index.js
- .env
- controllers/
- database/
- models/
- public/
- routes/
- views/
- Initiate node project
- Open terminal
- navigate to the root folder (the ls command should show the structure)
- use command
npm init -y
- Test that the server can run
- In terminal, use command
npm install express
- Write on
index.js
: -
- Import Express
-
- Create the
app
variable
- Create the
-
- Declare a port, and do
app.listen(PORT)
- Declare a port, and do
- In terminal, use command
node index.js
- Create views for client to see, using EJS
-
In terminal, use command
npm install ejs
-
Create
./views/index.ejs
-
Start with a boiler plate, and add a heading in the body to be seen
-
Create
./routes/viewRouters/
and./views/viewRouters/viewRouter.js
-
Write on
viewRouter.js
: -
- Import Express
-
- Create
router
variable
- Create
-
- Write a
router.get("/")
to respond with the ejs file
- Write a
-
module.exports = router
-
Write on
index.js
: -
- Import path
-
- Write Middleware for:
-
-
- Setting the view engine to EJS
-
-
-
- Set the views to look at the
./views/
- Set the views to look at the
-
-
-
- Use the
./public
folder for static files (CSS)
- Use the
-
-
- Import the router file
-
- Use the base URL when using
viewsRouter.js
- Use the base URL when using
-
Run the server and TEST IT
- Test basic connection to database
-
Write on
.env
file, define MONGODB_URI with your database connection string -
- Open up Compass
-
- connect to your cluster
-
- Copy connection string from the
...
in the top left
- Copy connection string from the
-
- Change the extension at the end to rename your database (
...mongodb.net/fullStack
)
- Change the extension at the end to rename your database (
-
In terminal,
npm install mongoose dotenv
-
Create
./database/mongodb.js
: -
- Import mongoose
-
require("dotenv").config()
-
mongoose.set('strictQuery', false);
-
- Create a connection function
-
- Export the connection function
-
On
index.js
: -
- Import
./database/mongodb.js
- Import
-
- Run the function in the callback of
app.listen()
- Run the function in the callback of
-
Run the server and TEST IT
- Set up a model, controller, and routes for a single collection
-
Create
./models/pokemonModel.js
: -
- Import mongoose
-
- Define a schema
-
- Model the schema
-
- Export the model
-
Create
./controllers/pokemonController.js
: -
- Import the Model
-
- Write a function that returns the entire collection
-
- Export the controller functions
-
Create
./routes/api/
folder -
Create
./routes/api/pokemonRouter.js
: -
- Import Express & create
router
variable
- Import Express & create
-
- Import the appropriate controller
-
- Write a GET method to extension
/allPokemon
- Write a GET method to extension
-
- Export the router
-
On
index.js
: -
- Import the
pokemonRouter.js
- Import the
-
- Use URL extension
/api
for this router
- Use URL extension
-
Create
./models/allPokemon.json
to insert the data via Compass -
Run the server, and TEST THE GET METHOD VIA POSTMAN
- Create an EJS view for ALL pokemon
-
Create
./views/allMons.ejs
: -
- Start with a boiler plate
-
- Use EJS
forEach()
to loop over data and generate elements for each property in thepokemons
collection:
- Use EJS
-
-
- PokedexNo - h1
-
-
-
- Name - h2
-
-
-
- Type - h3
-
-
-
- Moves - ul > forEach(li)
-
-
Create
./controllers/api/
and move thepokemonController.js
into it (update the import on./routes/api/pokemonRouter.js
) -
Create
./controllers/view/
-
Create
./controllers/view/viewController.js
: -
- Import the pokemonModel
-
- Define the async function
renderAllPokemon
- Define the async function
-
- Export the function
-
On
./routes/viewRouters/viewRouter.js
: -
- Import the viewController
-
- Write a GET method for URL extension
/allMons
, use therenderAllPokemon
as the callback
- Write a GET method for URL extension
-
Run server and TEST IT
- Create a back-end route for ONE pokemon using dynamic parameters
-
On
./controllers/api/pokemonController.js
: -
- Define function
getOnePokemon
- Define function
-
- Export
getOnePokemon
- Export
-
On
./routes/api/pokemonRouter.js
: -
- Import
getOnePokemon
- Import
-
- Write a GET method for URL extension
/onePokemon/:name
, use thegetOnePokemon
as the callback
- Write a GET method for URL extension
- Create a front-end view for ONE pokemon using dynamic parameters
-
Create
./views/oneMon.ejs
: -
- PokedexNo - h1
-
- Name - h2
-
- Type - h3
-
- Moves - ul > forEach(li)
-
On
./controllers/view/viewController.js
: -
- Define function
renderOnePokemonByName
- Define function
-
- Export
renderOnePokemonByName
- Export
-
On
./routes/viewRouters/viewRouter.js
: -
- Import
renderOnePokemonByName
- Import
-
- Write a GET method for URL extension
/oneMon/:name
, use therenderOnePokemonByName
as the callback
- Write a GET method for URL extension
-
On
./views/allMons.ejs
: -
- Wrap the names in an a tag, using
/oneMon/<%=mon.Name%>
as the href
- Wrap the names in an a tag, using
- Create a back-end route for CREATE one pokemon
-
On
./controllers/api/pokemonController.js
: -
- Define function
createOnePokemon
- Define function
-
- Export
createOnePokemon
- Export
-
On
./routes/api/pokemonRouter.js
: -
- Import
createOnePokemon
- Import
-
- Write a POST method for URL extension
/createOnePokemon
- Write a POST method for URL extension
- Create a front-end view for CREATE one pokemon
-
Create
./views/createMon.ejs
-
- Uses a form to collect what would normally be the
req.body
- Uses a form to collect what would normally be the
-
- Set up the form to make a POST request to URL
localhost:3000/api/createOnePokemon
- Set up the form to make a POST request to URL
-
On
./routes/viewRouters/viewRouter.js
: -
- Write a GET method for URL extension
/createOneMon
to render the view
- Write a GET method for URL extension
-
On
./controllers/view/viewController.js
: -
- Define function
renderCreatePokemonForm
to render the view only
- Define function
-
- Export
renderCreatePokemonForm
- Export
-
TEST IT, MAKE SURE THE VIEW SHOWS
- Connect back-end to receive front-end form data to CREATE one pokemon
- On
./controllers/api/pokemonController.js
: -
- Edit function
createOnePokemon
to accept form data
- Edit function
-
- Parse the Moves list to be an array instead of a string
-
- Redirect to
localhost:3000/oneMon/:name
- Redirect to
- Create a back-end route to DELETE one pokemon
-
On
./controllers/api/pokemonController.js
: -
- Define function
deleteOnePokemon
- Define function
-
- Export
deleteOnePokemon
- Export
-
On
./routes/api/pokemonRouter.js
: -
- Import
deleteOnePokemon
- Import
-
- Write DELETE method for URL extension
/api/deleteOnePokemon/:name
- Write DELETE method for URL extension
- Create a front-end button to DELETE one pokemon from the database
- On
./views/oneMon.ejs
: -
- Make a Delete button on the bottom of the page. It should send to the backend delete method via the form's action
- Connect the back-end to respond to front-end request appropriately
-
In Terminal:
-
- Use command
npm install method-override
- Use command
-
On
index.js
: -
- Import Method Override at the top
-
- Use the middleware as such:
app.use(methodOverride('_method'));
- Use the middleware as such:
-
On
./views/oneMon.ejs
-
- Use
?_method=DELETE
at the end of the form's action attribute
- Use
-
- Use
name="_method"
inside the delete button
- Use
-
On
./controllers/api/pokemonController.js
: -
- Redirect client to the
/allMons
URL extension# Pokemon-app
- Redirect client to the