Express ip filter for specific routes?

前端 未结 3 831
难免孤独
难免孤独 2021-02-09 20:44

Is it possible to apply different ip filters to different routes?

For example, I want only people from 123.123.123.123 can access my server\'s /test route,

3条回答
  •  你的背包
    2021-02-09 21:26

    In your main file where u defined app,

    app.use('/test',require('./whereever-my-route-is-located-where /test routes '));
    app.use('/',require('./wherever-my-this-routes-are-located'))
    

    in your route file .

    var express = require('express'),
        router = express.Router();
    //Ip verification for all requests : for whereever-my-route-is-located-where /test routes 
    router.use(function(req, res, next) {
        //verify Ip Logic
    });
    //this will be called for every route u define in that file, if it fails.
    

提交回复
热议问题