TL;DR
A website can make an HTTP request to localhost, but it’s tricky due to the browser’s security features (the same-origin policy). Common workarounds include using a proxy server on your web server or enabling CORS (Cross-Origin Resource Sharing) on the localhost service. The best approach depends on what you’re trying to achieve.
Why it’s normally blocked
Browsers have a security feature called the same-origin policy. This means a web page can only make requests to the same domain (protocol, host, and port) as the page itself. This prevents malicious websites from stealing data from other sites you’re logged into.
localhost is considered a different origin than most publicly hosted websites. Therefore, direct requests are blocked by default.
How to make it work
- Using a Proxy Server
- The simplest solution for development is often setting up a proxy server on your web server. Your website sends the request to your own server, which then forwards it to localhost.
- This bypasses the browser’s restriction because the initial request comes from the same origin as your website.
- Example (Node.js with Express):
- In this example, all requests to your web server (port 8080) are proxied to localhost:3000.
- Enabling CORS
- If you control the service running on localhost, you can configure it to allow cross-origin requests using CORS (Cross-Origin Resource Sharing) headers.
- This tells the browser that your website is permitted to access resources from localhost.
- Example (Node.js with Express):
- This example enables CORS for all routes on the localhost server.
- JSONP (Older Method – Limited)
- JSONP is an older technique that works by dynamically creating a
<script>tag to load data from another domain. It only supports GET requests and has security limitations. It’s generally not recommended for new projects. - Browser Extensions
- Some browser extensions can temporarily disable the same-origin policy for development purposes. However, this is a security risk and should only be used in controlled environments.
const express = require('express');
const httpProxy = require('http-proxy');
const app = express();
const proxy = httpProxy.createProxyServer({});
app.all('*', (req, res) => {
proxy.web(req, res, { target: 'http://localhost:3000' }); // Replace 3000 with your localhost port
});
app.listen(8080, () => console.log('Proxy server listening on port 8080'));
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors()); // Enable CORS for all routes
app.get('/data', (req, res) => {
res.json({ message: 'Data from localhost' });
});
app.listen(3000, () => console.log('Server listening on port 3000'));
Important Considerations
- Security: Be very careful when enabling CORS or using proxies, especially in production environments. Only allow requests from trusted origins to prevent cyber security vulnerabilities.
- Development vs. Production: The best solution for development (e.g., a proxy server) might not be suitable for production.
- HTTPS: If your website uses HTTPS, the localhost service must also use HTTPS for CORS to work correctly in some browsers.

