Skip to content

Latest commit

 

History

History
88 lines (58 loc) · 5.54 KB

readme.md

File metadata and controls

88 lines (58 loc) · 5.54 KB

Node HTTP Server

pet-shop

A local pet shop keeps a database for all the pets they have in stock. However, they need you to build a server application in Node to handle a variety of HTTP requests and send an HTTP response back.

The HTTP requests will be commands in the form of create, read, update, and destroy (CRUD). These commands will manage their database, which is the same JSON-formatted pets.json file. Once the command is correctly handled, the app will need to send an appropriate HTTP response back. Details of the HTTP requests that the app will need to handle and their expected HTTP responses can be found below.

Getting started

Change into the project directory.

$ cd path/to/project

All work will be done in the http.js file.

Assignment

Your task is to build a Node server application that handles the following HTTP requests and sends back the correct HTTP response. Where appropriate, your application must read the correct data from the pets.json file and include it in the response body. Additionally, your application must send the appropriate response status code and Content-Type header.

Request Method Request URL Response Status Response Content-Type Response Body
GET /pets 200 application/json [{ "age": 7, "kind": "rainbow", "name": "fido" }, { "age": 5, "kind": "snake", "name": "Buttons" }]
GET /pets/0 200 application/json { "age": 7, "kind": "rainbow", "name": "fido" }
GET /pets/1 200 application/json { "age": 5, "kind": "snake", "name": "Buttons" }
GET /pets/2 404 text/plain Not Found
GET /pets/-1 404 text/plain Not Found

To test your HTTP server, first install the nodemon package globally.

$ npm install -g nodemon

Start the HTTP server with nodemon.

$ nodemon http.js

You can use your web-browser or a tool like Thunder Client or Postman to test your routes.

Bonus

See if you can simplify your route handler with the following regular expression.

const petRegExp = /^\/pets\/(.*)$/;

You may find the following regular expression methods useful.

  • [RegExp.prototype.test()]['test']
  • [String.prototype.match()]['match']

Be mindful about responding to indices that don't exist in the database.

Request Method Request URL Response Status Response Content-Type Response Body
GET /pets/9000 404 text/plain Not Found
GET /pets/abracadabra 404 text/plain Not Found

Bonus

Add a catch all route handler for unknown HTTP requests and send the appropriate response.

Request Method Request URL Response Status Response Content-Type Response Body
GET / 404 text/plain Not Found
GET /blah 404 text/plain Not Found

Bonus

In future parts of this assignment, your server will need to handle create, update, and destroy HTTP commands. For now, add a route handler that responds to create commands by adding new pets to the pets.json file.

Request Method Request URL Request Body Response Status Response Content-Type Response Body
POST /pets { "name": "Cornflake", "age": 3, "kind": "parakeet" } 200 application/json { "name": "Cornflake", "age": 3, "kind": "parakeet" }
GET /pets/3 N/A 200 application/json { "name": "Cornflake", "age": 3, "kind": "parakeet" }

If age, kind, or name are missing from the HTTP request body or age is not an integer, then the data must not be added to the database and the server must send back the follow HTTP response.

Request Method Request URL Request Body Response Status Response Content-Type Response Body
POST /pets { "name": "", "age": "two", "kind": "" } 400 text/plain Bad Request
GET /pets/4 N/A 404 text/plain Not Found