HAPI、ヴィジョン、そして・・・誰?



今、hapiのテーマになっているようです.それは私にとって新しいです、そして、Googleの結果で表現するより少しよくカバーされているようです.

問題


私はNavbarのために通常のことをしようとしていました.しかし、どのように表示するかを知るには?loggedInの呼び出しごとに文脈にh.view()を追加したくない.

私が見つけた解決策


サーバービューの設定時には、you can supply a context to Vision :

The global context option can be either an object or a function that takes the request … The contents of the context will be merged with anything else you supply to a page being rendered.


そのために-
function buildVisionContext(request) {
  return {
    loggedIn: request.auth.isAuthenticated
  };
}

async function registerVision(server) {
  let cached;

  if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
    cached = false;
  } else {
    cached = true;
  }
  server.log(["debug"], `Caching templates: ${cached}`);
  server.views({
    engines: { ejs: require("ejs")},
    relativeTo: __dirname + "/../",
    path: 'templates',
    isCached: cached,
    context: buildVisionContext
  });
}

そして今、h.view()でレンダリングされたすべてのページが自動的にその変数が利用可能になります.
Markus SpiskeUnsplashによる写真