如何限制用户访问ExpressJS / NodeJS中的静态HTML文件? [英] How do I restrict the user from accessing the static html files in a ExpressJS/NodeJS?
问题描述
我想要所有到任何HTML文件(* .html)的路由,检测它并希望它被重新路由到一个错误页面。
$ b
PS:使用Express 4和最新的Node.JS版本。
app.get('/',function(req,res){
if(req.is('text / html')== true){
res.redirect('/ login');
}
这似乎没有在工作中,我想我错过了获取请求中的参数。
请指导,谢谢。
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屋!