How can I restrict user access to static html files in ExpressJS / NodeJS?

Is there a way to detect the url of the user request and redirect to a specific error page, like I want all routes to be in any HTML file (* .html), kind of detecting it and that it be redirected to an error page. Also hide it from the user to view it.

PS: Using Express 4 and the latest version of Node.JS.

app.get('/', function(req,res){
    if(req.is('text/html') == true){
        res.redirect('/login');
    }

      

This doesn't seem to work, I think I'm missing the parania in the get request.

Please guide. Thank you in advance.

0


source to share


1 answer


Suppose you have text html

in your url, and whenever it does, you want to direct it to login

. Try the following:

app.use(function (req, res, next) {
    if ((req.path.indexOf('html') >= 0)) {
        res.redirect('/login');
    } 
});

      



It checks your url and if it detects html

it redirects tologin

+2


source







All Articles