This is an activity designed to follow the REST API topic
You should create an Express app. All of this can be in-memory, no database or file storage needed.
The data consists of mailing lists which have a name and an array of member email addresses.
/lists
- fetch all the existing list names
const lists = new Map();
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
// add some fake data
app.get('/lists', (req, res) => {
const listsArray = Array.from(lists.keys()); // Why is this like this? Try it out in your console.
res.send(listsArray);
});
/lists/:name
- get list by name, e.g. /lists/staff
/lists/:name
- delete list by name
None needed
/lists/:name
- add or update a list with the given name
None
What if the name in the path doesn’t match the one in JSON body?? Sounds like an error case to me. Detect this case and pick an appropriate status code. (hint: if the client did something wrong in the request, it’ll be 4xx)
Use Postman to test retrieving, saving, and deleting mailing lists using your API.
Some would say the more REST-ful way to manage the members of the list is to make them into a resource.
After creating your pull request in your assignments repo, try this challenge!
GET /lists/:name/members
=> return array of emails only for a listPUT /lists/:name/members/:email
=> make the supplies email a member of the listDELETE /lists/:name/members/:email
=> remove the supplied email as a member of the listAPI’s often use POST and PUT for updates. They have slightly different meanings. This side-by-side comparison can be helpful but don’t get too hung up on that at this stage.
POST /lists/:name/members
with a body containing an email will add it to the list, even if it’s already there