Your First Server & Route
Initialize an HTTP server and create a simple route.
Learning Objectives
- Initialize HttpServer
- Bind to a port
- Create a GET route
##Creating the Server
Actix Web requires a runtime to execute asynchronous tasks. We use the actix_web::main macro to set up this runtime (which is built on top of Tokio).
The HttpServer::new function takes a closure that returns an App instance. The App is where you register your routes.
#Creating a Route
A route is simply a function that handles an incoming request and returns a response. In Actix Web, we use the #[get("/")] macro or the web::get() method to define routes. Let's look at the interactive code inspector to understand how they fit together.
Interactive Code Inspector
Select any highlighted line of code below to deeply inspect its mechanics, understand why it exists, and what happens if you change it.
"What If?" Experiment
Learning by doing. Modify the code to see what happens.
Change how many workers run.
Add .workers(2) before .bind().
The server runs with two worker threads instead of one per CPU core.
Explanation: Each worker has its own App instance. More workers handle more concurrent connections until you run out of CPU.
Common Mistakes
Anticipate and avoid these frequent pitfalls when working with this concept.
Port already in use
Another program (or an earlier copy of your server) is still listening on that port.
Explanation: Stop the other process or choose a different port. The ? on bind makes the program exit with the error instead of hanging.
Binding to 127.0.0.1 inside Docker
127.0.0.1 only accepts connections from inside the container itself.
Explanation: Use 0.0.0.0 in containers so the published port can reach your server, and keep 127.0.0.1 for local development.
Practical Exercise
Add a second route
TaskExtend the server with GET /health that returns the text "ok".
Requirements
- Keep the existing GET / route
- Add GET /health returning "ok"
Need a hint?
Try solving the exercise in your local environment first. Once you have a solution, or if you get completely stuck, reveal the answer.
Real-World Application
A /health endpoint like this is what load balancers and Kubernetes probe to decide whether your service is alive, so almost every production Actix service exposes one.
Key Takeaways
- HttpServer::new takes a closure that builds an App for every worker.
- bind() opens the port and run().await keeps the server alive.
- Register routes with #[get] style macros and .service(), or with .route().
- Visit http://127.0.0.1:8080 to see your server respond.
Finished reading?
Mark this lesson as done to track your progress and earn XP.