3

私はたくさんのミドルウェアを持っています。最初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を提供するにはどうすればよいですか。

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 に答える