javascript - Restrict access to Node.js using Express -
i have running node.js script located in server. want doesn't directly accessed browser , want domains/ip-s can call it! possible?!
not sure how distinguishing between accessing browser opposed other software, restricting access domains/ips should doable. following (non production) code restrict access localhost loop might serve starting point:
function securitycheck( req, response, next) { var callerip = req.connection.remoteaddress; (callerip == "::ffff:127.0.0.1" || callerip == "::1") ? next() : reply404( response); } function reply404( response) { response.writehead(404, {"content-type": "text/html"}); response.statusmessage = "not found"; console.log("reply404: statuscode: " + response.statuscode); response.end('<span style="font-weight: bold; font-size:200%;">error 404 – not found<\/span>'); } var app = express(); app.use(securitycheck); // restrict access ... // continue app routing
see more detailed answers express.js: how remote client address , how domain originating request in express.js?
Comments
Post a Comment