This sprint we will:
- focus on Create
- build a form to save albums into our database
- add a route to our server so that it can receive the form's data and create the album
Note: if you get stuck going through this, make use of the hints, your neighbors, and the solutions for sprint 2.
You must complete all of the previous sprint before starting this sprint (excluding stretch challenges).
-
Open
views/index.html
in your text editor. -
Edit the file by adding a new container and row after the jumbotron.
-
Use bootstrap to create a form to input 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
. When the form is submitted, use jQuery to capture the form values and serialize them. Start with aconsole.log
of the output. Its format should be similar to this sample serialized form data:
name=Marble+House&artistName=The+Knife&releaseDate=2006&genres=electronica%2C+synth+pop%2C+trip+hop
- Reset the form input values after the data from the form has been captured or used.
Next, let's add the correct RESTful route on the server. We already know that POST is used to create a new resource. If we're following good conventions, we'll use the same path that we did to retrieve all the albums.
POST /api/albums
- In
server.js
, add a new route after the currentGET
/api/albums
route. Make a newcreate
function in your albums controller to handle this route. Write a few comments to remind yourself what this route should do. Start byconsole.log
ing a message in this route. For a response, start by sending back the data the server received.
Don't forget to export the
create
function from the controller, or it won't be accessible inserver.js
.
-
If you haven't yet, add the
body-parser
middleware to the server. Remember savebody-parser
into your project's dependencies when you install it. -
You can test your new route by using AJAX (from your browser's JavaScript console), curl, or Postman.
curl (from Terminal):
curl -X POST http://localhost:3000/api/albums --data "name=Marble+House&artistName=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 add key-value pairs.
-
In the client-side JavaScript, set up your form handler to make a request to the proper route with the form data. Use AJAX.
-
Verify the proper message is getting logged by the server when you submit the form.
-
On the server side, break the data we're getting from the
genre
field into an array.
Hint: the
split
method may be handy here.
-
Connect the new route's controller to the database. Make sure you're returning the newly created album.
-
Test it!
Hint: if you get stuck here, take a look at the solution branch for sprint 2.
-
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. For example, require that a user fill in all the fields before your app processes the form. (What do you think should happen if the user submits the form with empty fields?)
-
Convert the form to a modal, and add a link to the right-side of the "Albums" header to open it!