🔧 Objective: Learn to set up a basic HTTP server that responds to GET requests.
🌐 Note: This guide assumes you're using Python 3. If you're on another platform, check out our tutorial on handling GET requests for more details.
Step-by-Step Guide
Install Dependencies
For Python, no installation is needed—use the built-inhttp.server
module.
For Node.js, run:npm install express
Write Server Code
Here's a minimal Python example:from http.server import BaseHTTPRequestHandler, HTTPServer class SimpleServer(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write(b'Hello, World!') if __name__ == '__main__': server = HTTPServer(('localhost', 8000), SimpleServer) print('Server running on port 8000...') server.serve_forever()
Run the Server
Execute the script and navigate tohttp://localhost:8000
in your browser. 🌐
Tips & Extensions
- 📌 To handle more complex routes, explore the HTTP methods tutorial.
- 🧪 Test your server with tools like Postman or curl for advanced scenarios.