简体   繁体   中英

How do I restrict the user from accessing the static html files in a ExpressJS/NodeJS?

Is there any way to detect the request URL of the user and reroute to a particular error page, like for example, I want all the routes to any HTML file (*.html), kind of detect it and want it to be rerouted to an Error page. Also hide it from the user to view it.

PS: Using Express 4 and Latest Node.JS version.

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

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

Please guide. Thanks in Advance.

Assume that you have the text html in your url, and whenever this is the case you want to route it to login . Try following:

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

It checks your url path, and if it detects html it will redirect to login

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM