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

天涯浪子 提交于 2019-12-13 04:47:55

问题


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.

P.S: 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.


回答1:


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



来源:https://stackoverflow.com/questions/28362909/how-do-i-restrict-the-user-from-accessing-the-static-html-files-in-a-expressjs-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!