是否可以将不同的ip过滤器应用于不同的路由?
例如,我只希望123.123.123.123的人可以访问我的服务器/test
路由,只有来自124.124.124.124的人才能访问我服务器的/
路由.
我知道express-ipfilter可以通过IP地址限制站点访问.但它不能将过滤器应用于特定路线.
我也知道app.use(ipfilter(ips, {}));
在路由中间添加只能对以下路由应用过滤器:
var express = require('express'), ipfilter = require('express-ipfilter').IpFilter; var ips = ['::ffff:127.0.0.1']; var app = express(); app.get('/test', function(req, res) { res.send('test'); }); app.use(ipfilter(ips, {})); // the ipfilter only applies to the routes below app.get('/', function(req, res) { res.send('Hello World'); }); app.listen(3000);
但我想为不同的路线使用不同的过滤器.
是否有可能做到这一点?
警告:包
express-ipfilter
现已弃用.
您可以链接中间件(而ipFilter是一个中间件).有两种方法可以做到这一点:
var express = require('express'), ipfilter = require('express-ipfilter').IpFilter; var ips = ['::ffff:127.0.0.1']; var testers = ['1.2.3.4']; var app = express(); app.get('/test', ipfilter(testers, {mode: 'allow'}), function(req, res) { res.send('test'); }); // the ipfilter only applies to the routes below app.get('/', ipfilter(ips, {mode: 'allow'}), function(req, res) { res.send('Hello World'); }); app.listen(3000);
或者限定使用中间件:
var express = require('express'), ipfilter = require('express-ipfilter').IpFilter; var ips = ['::ffff:127.0.0.1']; var testers = ['1.2.3.4']; var app = express(); app.use('/test', ipfilter(testers, {})); // the ipfilter only applies to the routes below app.get('/test', function(req, res) { res.send('test'); }); app.use('/', ipfilter(ips, {})); // the ipfilter only applies to the routes below app.get('/', function(req, res) { res.send('Hello World'); }); app.listen(3000);