HAPI、ヴィジョン、そして・・・誰?
4339 ワード
今、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 SpiskeのUnsplashによる写真
Reference
この問題について(HAPI、ヴィジョン、そして・・・誰?), 我々は、より多くの情報をここで見つけました https://dev.to/arafel/hapi-vision-and-who-am-i-k93テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol