Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Your First Server With node:http (Summary)

You’ve finished the course. You can now write a server with Node’s node:http module that routes requests by method and path, reads JSON bodies, and answers every request with the right status code and a JSON body.

Mrs. Okafor’s Questions, Answered

Your plot board answers the questions Mrs. Okafor used to get by phone. Node calls your listener once for every request, and each request gets exactly one response, with a status you chose. Here’s how the finished board answers the requests the course built it for:

Request Answer
GET /plots 200 OK and all 24 plots
GET /plots?vacant=true 200 OK and plots 4, 13, and 20
GET /plots/7 200 OK and plot 7
GET /plots/99 404 Not Found, code plot_not_found
GET /waitlist 405 Method Not Allowed with Allow: POST
POST /waitlist with Zoë Martin’s entry 201 Created and the saved entry
POST /waitlist without a JSON Content-Type 415 Unsupported Media Type, code wrong_content_type
POST /waitlist with a body that’s too big 413 Content Too Large, code body_too_large
POST /waitlist with broken JSON 400 Bad Request, code invalid_json
POST /waitlist with valid JSON but no name 422 Unprocessable Content, code invalid_entry

The listener parses the URL, then routes. Your readJson() helper checks the body in the order of the table. It reads the Content-Type header before any of the body arrives, counts the size while its chunks of bytes arrive, and parses the JSON only once the whole body is in. Broken JSON gets a 400, and valid JSON that breaks the endpoint’s rules gets a 422.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.