
Simplicity is the ultimate sophistication, and that's especially true when building a proxy server with Node.js. What we are creating is both powerful and surprisingly simple. It is a full-fledged proxy server capable of handling HTTP and HTTPS, blocking specific URLs, and serving your clients seamlessly. The best part is that even beginners can follow along.
Step 1: Install Node.js
First things first, install Node.js. On Ubuntu, the easiest method is using curl with NodeSource.
Update your system:
sudo apt update
Install curl:
sudo apt install curl
Download the Node.js package setup and Check your installation:
node -v
If a version shows up, you're ready. Easy, right?
Step 2: Build a Basic Proxy
Now, let's start simple. Open your favorite editor and create a file proxy.js:
var http = require('http');
var proxy = require('http-proxy');
proxyServer = proxy.createProxyServer({ target: 'http://127.0.0.1:9000' });
proxyServer.listen(8000);
server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Proxy Request was Successful!\n' + JSON.stringify(req.headers, true, 2));
res.end();
});
server.listen(9000);
Run it:
node proxy.js
Your proxy server is live on port 8000, serving content from port 9000. Configure Firefox to use it under Network Settings → Manual Configuration, and test by entering any HTTP URL.
Step 3: Deliver Website Content
The basic proxy works, but it only sends a simple text message. Let's make it actually forward content from requested websites:
var http = require('http');
var proxy = require('http-proxy');
var proxyServer = proxy.createProxyServer({});
proxyServer.listen(8000);
var server = http.createServer(function(req, res) {
console.log(req.url);
proxyServer.web(req, res, { target: req.url });
proxyServer.on('error', function(e) {
console.log("Error in proxy call:", e);
});
});
server.listen(9000);
Now your browser will actually see the full website content. Try visiting http://espncricinfo.com. Magic.
Step 4: Prevent URLs
Want to stop users from wasting time on Facebook? Or block unsafe sites? Node.js makes it trivial.
Create a file blocked.txt and list URLs to block. Example:
http://facebook.com
Update your proxy server to check this file:
var fs = require('fs');
var blocked = [];
fs.watchFile('./blocked.txt', function() { updateBlockedList(); });
function updateBlockedList() {
console.log("Updating blocked list...");
blocked = fs.readFileSync('./blocked.txt').toString().split('\n')
.filter(line => line.length)
.map(line => RegExp(line));
}
http.createServer(function(req, res) {
for (var i in blocked) {
if (blocked[i].test(req.url)) {
console.log("Blocked:", req.url);
res.end("Access Denied");
return;
}
}
});
Now, anyone trying to access http://facebook.com gets blocked. Simple and effective.
Step 5: Enable HTTPS Support
HTTP-only proxies are outdated. Today, almost every major website uses HTTPS. Let's upgrade:
var fs = require('fs');
var http = require('http');
var https = require('https');
var httpProxy = require('http-proxy');
var options = {
ssl: {
key: fs.readFileSync('valid-key.pem'),
cert: fs.readFileSync('valid-cert.pem')
}
};
var proxyServer = httpProxy.createProxyServer({});
proxyServer.listen(8000);
var server = https.createServer(options.ssl, function(req, res) {
console.log("HTTPS request:", req.url);
proxyServer.web(req, res, { target: req.url });
proxyServer.on('error', function(e) {
console.log("Error in proxy call:", e);
});
});
server.listen(9000);
Configure your browser to use this proxy for all requests, and now you can access https://google.com and other secure sites.
Conclusion
You've set up a fully functional HTTP/HTTPS proxy server with URL blocking. You can log traffic, cache responses for faster loading, add user authentication, and expand your filtering rules. Node.js gives you the freedom to scale and customize, making your proxy server ready for real-world deployment and full control over your traffic.
Language







Flux Stream Network Limited
RM A5,7/F, ASTORIA BUILDING, NO.34 ASHLEY ROAD, TSIM SHA TSUI, HONG KONG