This sprint we will:
- focus on Create
- build a form to save Albums into our database
- add a
.post
method to our server so that it can receive the form's data
Note: as we go through this if you get stuck make use of the hints, your neighbors and the solutions.
You must complete all of the previous sprint before starting this sprint. (excludes stretch challenges)
-
Open
views/index.html
-
Edit the file adding a new container and row after the jumbotron.
-
Use bootstrap to create a form to input your Album info. Follow the fields we've already used in our database.
Hint: You can build your own form or use some pre-made html.
- Edit your
app.js
. Use jQuery to capture the form values and serialize them.console.log
the output.
sample serialized form data:
name=Marble+House&textinput=The+Knife&releaseDate=2006&genres=electronica%2C+synth+pop%2C+trip+hop
- Clear the form after getting the data.
Let's add a post route on the server now. We already know that POST is used to create a new resource. If we're following good conventions we'll use the same URL that we did to retrieve all the albums.
POST /api/albums
-
In
server.js
, after the currentGET /api/albums
add a new route. Start by justconsole.log
ing the output and returning the same data you received as json. -
Add body-parser to the server.
-
You can test this by either using AJAX from your browser's javascript console, or by using curl or postman.
curl:
curl -X POST http://localhost:3000/api/albums --data "name=Marble+House&textinput=The+Knife&releaseDate=2006&genres=electronica%2C+synth+pop%2C+trip+hop
Hint: If using postman to POST set the BODY type to x-www-form-urlencoded, then set key-value pairs.
-
In the client-side JS, setup your form handler to make an AJAX post with the data.
-
Verify it's getting logged by the server when you submit.
-
On the server-side break the data we're getting in the
genre
field into an array.
-
Connect the POST route to the database. Make sure you're returning the new album.
-
Test it!
Hint: if you get stuck here take a look at the solutions.
-
When your server returns JSON, display it on the page. We already have a function to render it!
-
TEST ALL THE THINGS
-
Add HTML5 form validations to the form. Ensure that all fields are filled.
-
Change the form, replacing the textarea for genre with a field that has a button to add a new field for each genre. See the mockup:
- Convert the form to a modal and add a link to the right-side of the "Albums" header to open it!