如何限制用户访问ExpressJS / NodeJS中的静态HTML文件? [英] How do I restrict the user from accessing the static html files in a ExpressJS/NodeJS?

查看:293
本文介绍了如何限制用户访问ExpressJS / NodeJS中的静态HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检测用户的请求URL并重新路由到特定的错误页面,例如
我想要所有到任何HTML文件(* .html)的路由,检测它并希望它被重新路由到一个错误页面。
$ b

PS:使用Express 4和最新的Node.JS版本。

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

这似乎没有在工作中,我想我错过了获取请求中的参数。



请指导,谢谢。

html ,并且每当你想将它路由到 login 。试试看:

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

它检查你的url路径,如果它检测到 html 它会重定向到登录


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.

解决方案

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

这篇关于如何限制用户访问ExpressJS / NodeJS中的静态HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆