阻止对expressjs app的公共访问

Cur*_*ind 3 javascript firewall node.js express

考虑一下,以下expressjs app:

var express = require('express');
var http    = require('http');
var httpApp = express();

httpApp.configure(function() {
    httpApp.use(express.static(__dirname + '/static/'));
});

var server = http.createServer(httpApp).listen(4444);
Run Code Online (Sandbox Code Playgroud)

现在,我希望此服务器不会公开,只能根据其IP地址/域响应特定客户端的请求.其他人都应该得到403 - Forbidden错误.

我搜索了API Doc&found方法,首先启用信任代理,app.enable('trust proxy')然后检查req.ip.

但是,我无法访问req对象.所以如果有人拿这个代码并且可以告诉我如何基于其IP /域拒绝req,那将是非常有帮助的

ede*_*def 7

只需添加一个检查IP的中间件,如果不匹配则拒绝访问:

app.use(function(req, res, next) {
  if (allowed(req.ip))
    next();
  else
    res.status(403).end('forbidden');
});
Run Code Online (Sandbox Code Playgroud)