When starting your full stack development journey with CodeOp or anywhere else for that matter, you might often come across the term localhost and the port number 8080.
Localhost is your machine’s web server during development. It allows you to test websites and applications in a safe, controlled environment.
In lay terms, the combination of localhost and ports (like 8080) enables communication between your system and the web browser during testing.
Let’s properly understand localhost.
Simply put, localhost is your computer’s way of talking to itself. In networking terms, it refers to your machine acting as a server, allowing you to run applications locally rather than on a remote machine (server).
The IP address typically used for localhost is 127.0.0.1, which we call a loopback address.
When you run a web server on your machine (using technologies like Node.js, Apache, or Tomcat), your computer listens for incoming traffic directed to localhost. This allows developers to simulate how their applications will run on the web without needing an actual public-facing server.
If you’re a web developer and want to test the website or app you’re building, you don’t have to put it live on the internet right away. Instead, you can “host” it locally on your computer, making it accessible only to you using localhost.
There are three big reasons why every programmer insists on using localhost:
- It’s a safe environment to run and test code without exposing it to the public.
- There’s no need for network delays when accessing your local machine. It’s instant.
- Your work stays on your machine until you’re ready to deploy.
For example, when you type http://localhost in your browser’s address bar, your machine checks if a web server is running locally. If there is, it serves the files from your local machine just like a real web server would on the internet.
What is Port 8080?
In networking, ports are communication endpoints that allow devices to differentiate between various services or applications running on the same machine.
A computer can run multiple programs that need network access, so ports help direct the incoming and outgoing data to the correct service.
For example, I have a home server with a static IP address (192.168.1.25). I have about a dozen apps on this server, so I use ports to access them.
Here’s how it looks:
- Emby Media Player: 192.168.1.25:8096
- SABnzbd Downloader: 192.168.1.25:8010
- Portainer (for docker): 192.168.1.25: 8000
- Uptime Kuma: 192.168.1.25:8100
…and many others.
Port 8080 is one of the many ports a web server can use to listen to incoming requests. While port 80 is typically the default port for HTTP traffic (the same protocol that powers the web), 8080 is commonly used as an alternative, especially for development and testing environments.
Why 8080?
Port 80 is often already used by other services (like a production web server), so developers commonly use 8080 for testing without conflict.
Additionally, the number 8080 is easy to remember and is frequently used in documentation and examples for local development.
How to Access Localhost on Port 8080?
To access localhost on port 8080, you’ll first need a web server or application running locally and listening on this port.
Here’s a step-by-step guide to try and learn through practice:
Step 1: Start a Local Web Server
Starting a web server will differ depending on the programming language or framework you’re using. Here are a few common examples:
Node.js:
const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(8080, '127.0.0.1', () => { console.log('Server running at http://127.0.0.1:8080/'); });
Python: If you have Python installed, you can easily start a server in your current directory:
python3 -m http.server 8080
Apache/Tomcat: Many web servers like Apache or Tomcat are pre-configured to use port 8080 for testing purposes. Simply start the server, and it will listen on port 8080.
Step 2: Open Your Browser
Once the server runs, open a browser (like Chrome, Firefox, or Edge) and type http://localhost:8080 into the address bar. This URL tells your browser to connect to your local machine (localhost) using port 8080.
Step 3: View Your Application
You should see your web application or server response if everything is set up correctly. For instance, in the Node.js example above, your browser will display “Hello World” when visiting http://localhost:8080.
Here are some troubleshooting tips if something goes wrong:
- Server not running: If you get a “connection refused” or similar error, ensure your server is running and listening on port 8080.
- Firewall issues: Some firewall settings may block communication on certain ports. Make sure your firewall allows traffic on port 8080 for local connections.
- Port conflicts: Ensure that port 8080 is not already used by another service on your machine. If so, consider using another port (e.g., 3000, 5000, etc.).
Securing Localhost on Port 8080
Although localhost is typically considered safe because it’s restricted to your machine, there are some best practices to ensure that your local server is secure. This could be important if you intend to expose it to external networks.
1. Use HTTPS
While localhost doesn’t require HTTPS by default, using it can add an extra layer of security, especially when working with sensitive data. You can easily set up a local HTTPS server by generating a self-signed SSL certificate.
For example, in Node.js, you can use the https module:
const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') }; https.createServer(options, (req, res) => { res.writeHead(200); res.end('Secure connection established'); }).listen(8080, '127.0.0.1', () => { console.log('Server running at https://localhost:8080/'); });
This ensures that your local development environment mimics the security of a production server.
2. Configure Firewalls
Ensure that port 8080 is only accessible from your machine (localhost) and not from external devices unless necessary. You can do this by setting your firewall to block all external requests.
- Windows: Go to “Windows Defender Firewall” > “Advanced Settings” and configure rules to restrict access to port 8080.
- Mac/Linux: Use firewall tools like ufw or iptables to manage access on port 8080.
3. Limit Exposure with VPN or SSH Tunnels
If you need to expose your localhost server to external networks (for example, to show your work to a client or colleague), consider using a VPN or SSH tunnel.
My favourite tool for this is using Cloudflare’s tunnels through Cloudflare One. It is basically a network-as-a-service (NaaS) solution to establish secure remote connections.
These methods provide a secure way of sharing your local server without exposing it directly to the internet.
4. Authentication and Access Control
If you are working on more complex projects, consider adding basic authentication or token-based access controls to ensure that only authorised users can access your local application. This is especially important when exposing APIs or sensitive data.