Exobiology I
En route to Starbase Regula I on a routine mission, your starship encounters a previously uncharted planet in the Mutara Nebula. A science team is dispatched to the surface to catalogue the flora and fauna of the planet. You will be creating an express app for the uncharted planet.
Learning Objectives
- Full CRUD App
Prerequisites
- JavaScript
- Node / Express
Getting Started
- Create a project folder:
mkdir uncharted-planet
cd uncharted-planet
-
Create a new express app
npm init
touch server.js
- what other steps are there to setting up an express app?
New Route
Now that the planet is made, our starship can land! Let's make the planet inhabitable by creating a new route.
- Make a
new
route in yourserver.js
and test that it's working correctly by having itres.send('this is the new route')
- Make a views directory and
touch views/new.ejs
(NOTE: don't forget tonpm i ejs
! -
In the ejs file, make a
form
withinput
s- You will be creating the scientists that come to explore the planet, so include inputs for: name and description
- Back in your server file, update your
new
route so that it renders the new view page we just created
Create Route
Great, the planet is now inhabitable! Let's start sending the scientists in by making a create route.
- Make a
create
route in yourserver.js
- Remember to set up the
body-parser
middleware ... i.e.app.use(express.urlencoded(..
- Check to make sure your create route is working correctly and connected to your
new
form we made above by usingres.send(req.body)
- Once you're sure the create route is working, update your route so that the newly created data is being saved somewhere
-
Now, let's inhabit the planet! Create 5 scientists!
- For example, you can make a scientist with the name
Xxylox
that has a description ofA spiky armadillo-like creature that wants to snuggle you with its spikes
- For example, you can make a scientist with the name
Index Route
Now that the planet is inhabited, we should keep a directory of all the inhabitants. Let's create an index page
- Create an
index
route in yourserver.js
and test that the route is correct byres.send('index')
- Create an
index.ejs
file and upgrade yourindex
route so that it renders this file - Upgrade your
index
route andindex.ejs
files so that it displays all the scientists in your database
Delete Route
Uh oh! It turns out that the planet can't handle much and it's quickly reached capacity! We have to send some scientists away by creating a delete route.
- In your
index.ejs
add a delete form onto all the scientists - Don't forget to install and configure
method-override
- Create a
delete
route in yourserver.js
- Make sure the
delete
route deletes just the one specified object/scientist, then redirects to the index page
Hungry for More?
RUN! Your team has been ambushed by pirates intent on stealing your data! Quick, delete the "database" so they don't get their hands on it!
- Using a
delete
route, wire up a button that deletes all data at once!
Hungrier for Even More
- Create a
show
page/route for each scientist and link to them from the index page - Create categories for the different types of life. For example, store "Plants" and beneath that, "Flowers", and then individual entries.
- Add a "verified" flag to certain entries and prevent them from being deleted. After all, as a senior Exobiologist you know what you're looking at!