Adding an endpoint for data
Now that we've got a dynamic server backing our app, one thing we can do to improve our application is handle the data a bit better. Right now, we simply throw the list of pets in with the rest of the code, where it's hard to get at and hard to modify.
First, let's move our list of pets to a separate file. It's easier to maintain the data when it isn't mixed with application logic, and this will provide a nice separation of concerns for later. We'll create a new directory named data
and a new file named data/pets.coffee
. This file will contain our entire list of pets that used to be in assets/js/animal.coffee
:
module.exports = [ name: "Kelsey" type: "dog" age: 2 breed: "Labrador" description: "A sweet and loyal dog. Loves to play fetch. Sometimes drinks out of the toilet." #, ...
We're using the Node module.exports
declaration here and assigning to it the value of the entire array. It declares that requiring this file will expose this array to other files...