我有一堆中间件。一开始app.use
我测试该过程是否受到胁迫,如果是,我希望它只发送静态/index.html
文件并将用户的浏览器重定向到"/#" + req.url
.
例如:
app.set("port", PORT)
//etc
app.use(function(req, res, next) {
if (something)
res.sendfile('/public/index.html', { root: __dirname + '/..' })
// also, redirect the user to '/#' + req.url
else
next()
});
// a ton more middleware that deals with the main site
app.use(express.static(...))
现在,它只是将 index.html 发送到他们所在的任何 url。我怎样才能将它们重定向到“/”并提供index.html,而不会弄乱任何未来的中间件。
不确定我理解是否正确,但试试这个:
app.use(function(req, res, next) {
if (something && req.path !== '/')
return res.redirect('/');
next();
});
app.get('/', function(req, res, next) {
if (something)
return res.sendfile('/public/index.html', { root: __dirname + '/..' });
next();
});
app.use(express.static(...));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)