2016-02-05 14 views
5

Ho uno script node.js in esecuzione che si trova in un server. Quello che voglio è che non è direttamente accessibile dal browser e voglio anche che solo determinati domini/IP-s possano chiamarlo! È possibile ?!Limita l'accesso a Node.js utilizzando Express

+0

cosa hai provato? – CoMartel

+0

Per quanto ne so, Express non è in grado di eseguire blocchi condizionali propri come Nginx e Apache. Quindi sono fuori dalle idee al momento. –

risposta

1

Non so come distinguere tra l'accesso a qualcosa da un browser rispetto ad altri software, ma limitare l'accesso ad alcuni domini/IP dovrebbe essere fattibile. La (non di produzione) codice seguente per limitare l'accesso al localhost loopback potrebbe servire come punto di partenza:

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 &ndash; Not Found<\/span>'); 
} 
var app = express(); 
app.use(securityCheck); // restrict access 
... // continue with app routing 

Vedi anche più dettagliate risposte a SO Express.js: how to get remote client address e How do I get the domain originating the request in express.js?

Problemi correlati