You are developing a REST back end of an address book app. You were asked to implement endpoints to fetch a list of contacts (with phase matching and results limiting) and to fetch details of a single contact and to delete a given contact.
As the database is not ready, you are provided with a fake database layer exported
with ./src/database/fakeDatabase.js.
Your implementation has to:
- make all tests pass,
- modify helper functions if needed,
- fulfil the requirements of HTTP endpoints described below:
- The response HTTP status should be:
200 OK. - The response
Content-Typeheader should be:application/json. - The response body should be: a JSON array with details of all contacts as loaded from the database (each contact has an
ID, aname, aphoneand an array of stringsaddressLines). - The contacts in the response body are sorted by their name, which means a contact with
the name of
Abbigail Wunschwould be first and a contact with the name ofZoila Daugherty IIwould be last. - If the
phraseURL query parameter is present:- in the response body, filter out contacts with names not matching
phrase; - the
phraseand the contact'snameare compared lowercase, which means that bothTheresa GorczanyandZakary Mayermatch the followingphrase=zA; - If there are no contacts matching the
phrase, then the response body contains only an empty JSON array as there are no contacts to include in it. - If the
phraseis empty (phrase=), then the response has the400 Bad RequestHTTP status and an empty response body.
- in the response body, filter out contacts with names not matching
- If the
limitURL query parameter is present:- the number of the returned contacts should be limited by the
limitnumber;- e.g., if there are 3 contacts matching the
phraseparameter and thelimitis 10, then those 3 contacts are included in the response body; - e.g., if there are 10 contacts matching the
phraseparameter and thelimitis 3, then only the first 3 contacts (sorted by name) are included.
- e.g., if there are 3 contacts matching the
- If the
limitis not a valid non-negative integer (0,1,2and so on), then the response has the400 Bad RequestHTTP status and an empty response body.
- the number of the returned contacts should be limited by the
- If a contact with
IDvalue<contact-id>exists:- the response HTTP status should be:
200 OK; - the response
Content-Typeheader should be:application/json; - the response body should be: a JSON object with details of that contact loaded from
the database (
ID,name,phoneand an array of stringsaddressLines).
- the response HTTP status should be:
- If a contact with
IDvalue<contact-id>does not exist:- the response HTTP status should be:
404 Not Found.
- the response HTTP status should be:
- If a contact with
IDvalue<contact-id>exists:- the response HTTP status should be:
204 No Content; - that contact is deleted, which means it is no longer included in the response to
GET /contactsrequests and its details are no longer available when making theGET /contacts/<contact-id>request.
- the response HTTP status should be:
- If a contact with
IDvalue<contact-id>does not exist:- the response HTTP status should be:
404 Not Found.
- the response HTTP status should be:
- The response HTTP status should be:
200 OK. - The response
Content-Typeheader should be:text/plain. - The response body should be:
pong.
- The request to the URL path different than the expected ones would get the
404 Not FoundHTTP status. - The request to the supported URL path, but with an unexpected HTTP method would get the
405 Method Not AllowedHTTP status.
This app was originally created with Node.js 12.13.1 / npm 6.12.1. You can use
nvm to make sure you are working with the same
version of Node.js. Run nvm install and nvm will set up Node.js based on
the .nvmrc file.
Follow these steps to set up the app:
npm install– install dependencies.npm test– run all tests (should fail unless you implement the task).npm start– serve the app at http://localhost:8080/ (you can check if it works by opening the http://localhost:8080/ping in your browser or executing thecurl http://localhost:8080/pingin your terminal –pongshould be printed).
There is also the npm run test:watch command available to start the test runner in
the watch mode. It runs tests related to modified files only.
You can also use the npm run start:watch command to serve the app in the watch
mode. It restarts the server every time the code is modified.