3

I have a bunch of middleware. At the first app.use I test if the process is under duress, and if so I want it to just send the static /index.html file and redirect the user's browser to "/#" + req.url.

for example:

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(...))

Right now, it just sends the index.html to whatever url they're at. How can I redirect them to "/" and serve index.html without messing up any future middleware.

4

1 回答 1

7

不确定我是否理解正确,但试试这个:

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(...));
于 2013-11-11T16:11:56.230 回答