We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Server

We're building a fully-fledged web server from scratch on your local machine. The test suite will make HTTP requests to your local server over localhost. Your server will run in one terminal, while you submit tests with the Boot.dev CLI in another terminal.

http Package

The Go standard library makes it easy to implement a simple server. You just have to assemble the pieces in your code.

The http package already provides an http.Server for you. It needs an address, Addr, on which to listen for incoming requests. It also needs a Handler to handle incoming requests. We'll use an http.ServeMux to route incoming requests to its handlers. Because the ServeMux won't have any routes yet, it will respond to every request with 404 Not Found.

Start the server by calling its ListenAndServe method. It listens for incoming requests at the server's Addr and passes each one to the Handler.

Documentation

Being a programmer means reading documentation and searching for examples. We're already telling you everything you need to know to build Chirpy, but we don't want to rob you of valuable practice reading the docs and learning from examples. Be sure to read the http package's overview as a primer.

Reading documentation is an essential skill because a professional in any field should never stop learning. In fact, the best way to learn is on the clock.

Assignment

Build and run a server that listens on port 8080 and responds to every request with 404 Not Found.

  1. go build -o out && ./out
    

In another terminal window, while your server is still running, run and submit the CLI tests.

Tip

If the bootdev CLI requests a URL other than localhost:8080, your ~/.bootdev.yaml file may have the base_url set from a different course. Run bootdev config base_url --reset before running the tests again.