By the end of the challenges, you should have:
- A JSON API with a full set of RESTful routes, so you can CRUD (and search!):
- Create a new todo (
todos#create
) - Read/fetch a list of todos (
todos#index
) - Read/fetch a specific todo by id (
todos#show
) - Update a specific todo by id (
todos#update
) - Delete a specific todo by id (
todos#destroy
) - Bonus: Search for todos by title (
todos#search
)
- Create a new todo (
- Fork this repo, and clone it into your
dev
folder on your local machine. - Follow the instructions to setup your test suite (below)
- Run the tests, and try to make the red (failing) tests turn green (passing). Then move on to bonuses.
For this challenge, we'll be testing our JSON API using a testing framework called Mocha and assertion library called Chai.
The tests have already been written for us! Our goal is to try to understand the test output so that we can write server-side code that will make the red (failing) tests turn green (passing).
The tests will help guide our development process, and keep us focused on our end goal: a RESTful, full-CRUD, JSON API.
From inside your cloned directory, run:
npm install -g mocha # globally install the mocha test framework
npm install # download package.json dependencies for this project
Next, run your server:
node server.js
# or better yet
nodemon server.js
# or just
nodemon
Finally, open a new teminal tab/window, and run the test suite (you'll need to switch back and forth between your server output and your test output a lot):
mocha test/todosTest.js
# or just
mocha
Your server must be running in order for the Mocha tests to be able to make API requests to your API endpoints.
By the end of this assignment you'll hopefully see this glorious output:
Todos API
GET /api/todos (index)
✓ should respond with status 200 - Success
✓ should respond with a JSON object
✓ should respond with a JSON object containing a list of todos
✓ todo objects should have properities: _id, description, task
GET /api/todos/:id (show)
✓ should respond with status 200 - Success
✓ should respond with JSON
✓ should fetch one specific todo by _id
POST /api/todos (create)
✓ should respond with status 200 - Success
✓ should respond with JSON
✓ should respond with the new todo object
✓ should assign an _id to the new todo object
DELETE /api/todos/:id (destroy)
✓ should respond with 200 or 204 on success
✓ should delete one specific todo from the list of todos
PUT /api/todos/:id (update)
✓ should respond with status 200 - Success
✓ should respond with JSON
✓ should update the fields on one specific todo
GET /api/todos/search (search)
✓ should list all todos that contain the search term in their title
But for starters you'll see output that looks like this:
0 passing (84ms)
9 failing
1) Todos API GET /api/todos (index) should respond with status 200:
Uncaught AssertionError: expected 404 to equal 200
+ expected - actual
-404
+200
You can think of each failing test as being like a trail of breadcrumbs. If you pay close attention to the error messages, it should give you a clue about what to do next!
Why is the server responding with status 404 - Not Found
when we try to GET /api/todos
?
Take a look at server.js
and see if you can figure it out!
-
Build a way for a user to search for todos. You'll need:
- A new html page called
views/search.html
that's served from your application.- Make sure to connect it to
styles/main.css
andscripts/main.js
!
- Make sure to connect it to
- Can you consume the
/api/search
endpoint and display search results on the page? Can you get it to work with queries the user enters into an input field?
- A new html page called
-
Build a way for a user to mark a todo as "done". You'll need:
- A styling change on the client to indicate the todo is "done" (this should be persistent if the user refreshes the page)
- A request (AJAX) to mark the todo as done (update it) on the server
- As you make code changes, commit frequently and push to GitHub.
- Submit this lab as a pull request from your fork to the original repo.