NodeJS + Express: How to secure a URL

后端 未结 3 490
清歌不尽
清歌不尽 2021-01-29 23:48

I am using latest versions of NodeJS and ExpressJS (for MVC).

I usually configure my rest paths like this, for example:

app.get(\'/archive\', routes.arch         


        
3条回答
  •  逝去的感伤
    2021-01-30 00:10

    A even simpler approach would be to add the following code in the App.js file.

    var auth = function(req, res, next) {
    
        if(isAdmin) {
    
            return next();
    
        } else {
    
            return res.status(400)
    
        }
    };
    
    app.use('/admin', auth, apiDecrement);
    

    As you can see the middleware is being attached to the route. Before ExpressJS goes forward, it executes the function that you passed as the second parameter.

    With this solution you can make different checks before displaying the site to the end user.

    Best.

提交回复
热议问题