2. Your First Server (No Frameworks)
Your First Server
Now that you understand HTTP, let's build a REAL server. We'll start with NO frameworks — just the native HTTP module in Node.js or Python. This helps you understand what frameworks like Express or Flask do for you.
What Is a Port?
Your computer runs many programs at once. A port is like an apartment number. The IP address is the building address. The port tells the network which specific program should receive the request.
Standard HTTP (web traffic)
Standard HTTPS (encrypted web traffic)
Common for Node.js development
Common for Python development
When you see http://localhost:3000, it means: "Connect to my own computer (localhost), specifically the program listening on port 3000."
Your First Node.js Server (No Express)
Create a file called server.js:
// Node.js built-in HTTP module — no extra packages needed! const http = require('http'); // Create a server that responds to every request const server = http.createServer((req, res) => { console.log(`Received request: ${req.method} ${req.url}`); // Set the response status code and headers res.writeHead(200, { 'Content-Type': 'application/json' }); // Send the response body res.end(JSON.stringify({ message: 'Hello from your first server!', method: req.method, url: req.url })); }); const PORT = 3000; server.listen(PORT, () => { console.log(`Server is running!`); console.log(` Open http://localhost:${PORT} in your browser`); });
Run it: node server.js
Open your browser to http://localhost:3000. You'll see the JSON response!
Ctrl + C in your terminal. This sends an interrupt signal that kills the process.Your First Python Server (No Flask)
Python has a built-in HTTP server module. Create a file called server.py:
from http.server import HTTPServer, BaseHTTPRequestHandler import json class Handler(BaseHTTPRequestHandler): def do_GET(self): print(f"Received request: {self.command} {self.path}") self.send_response(200) self.send_header('Content-Type', 'application/json') self.end_headers() response = json.dumps({ 'message': 'Hello from your first server!', 'method': self.command, 'url': self.path }) self.wfile.write(response.encode()) PORT = 8000 server = HTTPServer(('localhost', PORT), Handler) print(f"Server is running!") print(f" Open http://localhost:{PORT} in your browser") server.serve_forever()
Run it: python server.py
Handling Different URLs (Routing)
A real server needs to do different things based on the URL. Here's how to check the request path:
// Node.js — checking different URLs const http = require('http'); const server = http.createServer((req, res) => { // Set the content type to JSON for all responses res.setHeader('Content-Type', 'application/json'); if (req.url === '/') { res.statusCode = 200; res.end(JSON.stringify({ message: 'Welcome home!' })); } else if (req.url === '/users') { res.statusCode = 200; res.end(JSON.stringify({ users: ['Alice', 'Bob', 'Charlie'] })); } else if (req.url === '/health') { res.statusCode = 200; res.end(JSON.stringify({ status: 'OK', timestamp: new Date() })); } else { res.statusCode = 404; res.end(JSON.stringify({ error: `Cannot ${req.method} ${req.url}` })); } }); server.listen(3000, () => console.log('Server running on port 3000'));
Knowledge Check
Ready to test your understanding of 2. Your First Server (No Frameworks)?