First Server

Creating a Litestar application and serving a response

Concepts

A minimal Litestar app

Routes and handlers

Returning plain text and JSON

Running and reloading

The request-response cycle

Check for Understanding

What does it mean for a server to "listen on port 8000"?

The operating system assigns each network service a port number. When a server "listens on port 8000", it registers with the OS to receive any incoming network connections directed at that port. Your browser connects to http://localhost:8000 by opening a connection to port 8000 on the local machine.

If you define two routes with the same path but different HTTP methods, will Litestar treat them as one route or two?

It will treat them as two separate routes. A route in Litestar (and in HTTP in general) is identified by both the path and the method. GET /snails and POST /snails are distinct routes that can have completely different handlers. This is normal: GET might retrieve a list, while POST creates a new item.

What happens in the browser if the server returns Content-Type: application/json but you expected HTML?

The browser will not render the response as a page. Depending on the browser, it will display the raw JSON text, offer to download it, or show it in a JSON viewer. The Content-Type header is how the browser decides what to do with the response body, so returning the wrong one produces confusing results.

Why does --reload mode watch for file changes, and why would you not use it in production?

In development, --reload restarts the server automatically whenever you save a file, so you do not have to stop and restart manually after every change. Automatic restarts are dangerous in production: a partial file save or a syntax error could take the server down, and the restart overhead adds latency. Production servers use a process manager that restarts only on deliberate deployments.

Exercises

Add a health-check route

Add a /health route that returns a JSON object containing the current server time. Confirm it works by visiting it in a browser and with curl. Prompt the LLM to write the handler, then read the code and identify which part produces the timestamp and which part controls the response format. Did you prompt the LLM to write the curl command?

Add a path parameter

Prompt the LLM to add a route /site/{site_name} that extracts the site name from the URL and returns it in a plain-text response. Try visiting /site/toronto in your browser and verify the response contains the name. What happens if you visit /site/ with no name?

Explore error handling

Visit a URL that is not handled by any route (e.g., /does-not-exist). Note what Litestar returns by default. Ask the LLM to add a custom 404 handler that returns a friendlier error message. Verify it works and check that it also sets the correct status code.

Read the server log

Run the server and make at least four requests: a successful GET, a 404, a route with a path parameter, and a request with a query parameter. For each line the server prints, write a one-sentence explanation of what it records, then compare your explanation with one from an LLM.