How can I restrict access by IP address in a Node.js HTTP server application?
I'm looking for something like this:
Deny from all Allow from ..
I need to allow access to the site for only a few IP addresses. How can I do this?
If you are using Express in your app, then you can use express-ipfilter or similar package in order to block access by ip. You can store the list of ips in memory or some kind of storage like Redis, and add ips dynamically (ex.
I'm not sure how bulletproof is this approach, but here it is, collected from answers around the web:
var http = require('http'); http.createServer(function (req, res) { var ip = req.ip || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress; if (ip == '127.0.0.1') // exit if it's a particular ip res.end(); ...
Please, someone more proficient in node - correct me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With