This sprint we will:
- focus on Read
- connect our partially pre-built front-end to a back-end with hard-coded data.
- replace the hard-coded data with data stored in a mongo db
Now would be a great time to explore the files provided for you. In particular note:
- the html in views/index.html
- the incomplete server in server.js
- the included package.json
Use nodemon throughout the exercise to run your server.
Continually verify that your browser console is displaying the app.js loaded!
message on document-ready.
Goal display hard-coded data from app.js
on index.html
Let's start on the outside and work our way in.
-
Open
index.html
and find the HTML for an album. Delete the HTML for all of the albums. Leave thediv.albums
in place. -
Open
app.js
and edit the functionrenderAlbum
to display one Album on the page. You should use HTML just like what you just deleted. Build-up the HTML string and use jQuery to render it on the page. -
Run the function on document-ready and give it
sampleAlbums[0]
(just one album). Verify that the page looks right. -
Update your code to use all the sampleAlbums. Use
forEach
.
hint: calling renderAlbum
$(document).ready(function() {
console.log('app.js loaded!');
renderAlbum(sampleAlbums[0]);
});
We're going to add the following index route on our server:
GET /api/albums
-
Open server.js and create a new route for
/api/albums
-
Serve the hard-coded albums in server.js on
/api/albums
. This is an API route, so let's send JSON. -
In
app.js
, useajax
to get the albums. Render them on the page.
The data in
server.js
andapp.js
is different; making it easy to see which data is being rendered on your page.
Let's setup the database now.
-
Use
npm
to installmongoose
. -
In
models/album.js
add a model for our albums. You should be able to determine the datatypes based on the sample data in the server. -
Export Album in
models/album.js
-
Require and export Album in
models/index.js
hint: `models/albums.js`
//models/album.js
var AlbumSchema = new Schema({
artistName: String,
name: String,
releaseDate: String,
genres: [ String ]
});
var Album = mongoose.model('Album', AlbumSchema);
module.exports = Album;
hint: `models/index.js`
module.exports.Album = require("./album.js");
Let's try seeding our database.
-
Move the hard-coded model data from
server.js
intoseed.js
. You'll note there's already an empty variable there for you to use. -
Strip out the pre-coded
_id
properties, mongo will take of creating these for us. -
Make sure
mongod
is running in a terminal. -
Run node seed.js
-
Resolve any errors you encounter.
hint: `error connect ECONNREFUSED`
If you see an error like:process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED 127.0.0.1:27017
It usually means that mongod
is not running.
Now that the database is seeded, let's continue and use it in our /api/albums
route.
-
Require
./models
inserver.js
. -
Edit the current
app.get('/api/albums', fun...
to access the database and pull all albums. -
Verify that you're getting the right data on your index page now. Your ajax should still work; but if the
keys
in the data have changed at all you'll have to resolve that.
hint: requiring `./models`
var db = require('./models');
If you're stuck, ask your senior dev for help.
If you've made it this far then we've created an API that has an index route /api/albums
.
Our app has a single-page view that makes an ajax GET request to the API and renders the information. Our data is being Read from the database by the server.
We've completed the Read component of our CRUD app for the moment.
Good job!