7.5 KiB
WDI-PANTHALASSA
Title: Vampires
Type: Homework
Duration: Single night
Creator: Thom Page
Course: WDIr-Panthalassa
Competencies: Read and Create data with four of the seven RESTFUL routes
Prerequisites: Express routing, EJS
VAMPIRES
The CR in CRUD
CRUD is an acronym for Create, Read, Update, and Destroy-- these are the fundamental actions we perform on data.
Tonight's homework is to make an app that will both Read and Create data for a single model (Vampires). Update and Destroy will come later.
APP
The app will have an index page, a show page, and a new page. It will also have a create route.
- Index: all of the vampires are displayed on this page (just the images, they are 300 x 300 px).
- Show: one particular vampire is displayed on this page.
- New: a form for creating a new vampire is displayed.
- Create: this is a POST route that adds data to the Vampires array (it does not have a webpage, instead it redirects)
The app will show a list of vampire images. You click on 'em, they go to a show page.
The app will also be able to store new vampires. You click on a 'new vampire' link and a new vampire is resurrected from obscurity and will appear on the index page.
DIRECTIONS
-
Use the
vampires_expressstarter code. -
Run
npm installto install all the dependencies that are already inpackage.json. -
requirethe Vampires data stored in themodelsfolder:
var vampires = require('./models/vampires.js');
Controllers
- Make your
indexandshowroutes for the vampires in the vampires model.
- URI convention for index: GET `/vampires`
- URI convention for show: GET `/vampires/:id`
** Commit your work.**
The commit message should read:
"Commit 1: index and show routes".
- Make a
newroute. All this should do is render thenewview, you don't need to pass it data.
- URI convention for new: GET `/vampires/new`
- Place your
newroute above yourshow route, or the user will never get to it (they will always go to theshowroute instead). Can you explain why this might be?
** Commit your work.**
The commit message should read:
"Commit 2: new route".
CREATE and req.body
- Make a
createroute.
- URI convention for create: POST `/vampires`
-
This will add the data sent from the
newform into the Vampires array, andredirectto theindex. This new data will come from the request object-- inside another object calledbody. Doesreq.bodyexist withoutbody-parser? Well no.body-parseradds in an empty body object to the request object that can later be populated with data. You can test the existence ofreq.bodyby console.logging req.body.-
You will need to set up
body-parserin yourserver.js. -
Once you have required body-parser, this is the code to get it to work:
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json());Note that you can find this code in the body-parser docs on the
npmjssite. -
** Commit your work.**
The commit message should read:
"Commit 3: create route".
Testing POST requests
-
Network Tab
You can see the POST request go through in the Network tab in the Chrome console. Open up the console, hit the submit button for the request, and you will see the requests load. Click on the POST request, select the Headers, and scroll down to the bottom where you will see the form data.
This is good way to see if the form data has even been passed through the browser.
-
Postman / cURL
You can use Postman or cURL to send data to a server. Send data with Postman or cURL to your POST route, and set up the route simply to console.log req.body to see if the data has reached the server.
Sample curl request, use it in any directory in terminal:
curl -X POST -d key=value -d key2=value2 http://localhost:3000/vampires
Sample route to check if data has arrived:
app.post('/vampires', function(req, res) {
console.log("HERE IS THE DATA: ");
console.log(req.body);
res.redirect('/vampires');
});
** Commit your work.**
The commit message should read:
"Commit 4: tested the create route successfully".
Views
index.ejsshould have clickable images of each of the Vampires. Note that the images in the model are the same size (300 x 300 px each) to avoid resizing. Each image should link to theshowpage for that Vamp. There should be a link toadd a new Vampire.
** Commit your work.**
The commit message should read:
"Commit 5: index view".
show.ejsshould display the Vampire's name, image, location, gender, and number of victims. There should be a link to return to the Vampires index.
** Commit your work.**
The commit message should read:
"Commit 6: show view".
new.ejsshould render a form where the user can enter a new Vampire's name, location, gender, number of victims, and url of an image. The form will submit to thecreateroute.
** Commit your work.**
The commit message should read:
"Commit 7: new view".
BONUS - STYLING
Add styling to your page
Express can access static assets like css files, images, and front-end javascript so that you can make your pages look and behave nicely.
First, tell express to look in the 'public' folder for static assets:
app.use(express.static('public'));
Next, create a public folder in the root of the project. Express will interpret the public folder as the root your static assets.
In your public folder make a style.css and add some stuff in the file.
Link your style.css in one of your views in the normal way with the link tag in the head of the html.
** Commit your work.**
The commit message should read:
"Commit 8: added static assets".
NOTES ON FORMS
- The
nameof each input box should exactly match the corresponding key name within the model. In the Vampires model,imgis the key that stores the image url. Therefore, in your form, the name of this input should also beimg. Example:
<form action="/vampires" method="POST">
<input name="img" placeholder="image url">
<button>Submit new vampire</button>
</form>
When this data is sent to the back-end, you won't need to change the name of your data to fit your model.
NOTES ON RESTFUL ROUTING
There are seven RESTFUL routes, but tonight we are only using four.
VIEW routes (GET):
indexdisplays an index of all resources.showdisplays just one resource.newprovides a form for adding a new resources.editprovides a filled-out form for an existing resource.
The routes that have views are sent from a GET request.
REDIRECT routes (POST, PUT, DELETE):
createtakes the data from thenewform.updatetakes the data from theeditform.destroyremoves data.
The routes that have redirects are sent from POST, PUT, and DELETE requests.
In an app with full RESTFUL routes, you will have **four** views and **three** redirects.
Later on, you could of course combine views to reduce navigation, but for now, there are four.
Tonight's homework will use three of these views, `index`, `show`, and `new`, and just one redirect, `create`.




