问题
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