如何在所有把手页面中获取Express Session变量,现在我每次都渲染它,我希望只完成一次 [英] How to get the Express Session variable in all the handlebars pages, right now i have render it each time, i want it to be done once only
问题描述
如何在所有车把页面中获取Express Session变量,现在我每次都渲染它,我希望它仅执行一次并从车把页面访问.
How to get the Express Session variable in all the handlebars pages, right now i have render it each time, i want it to be done once only and access in from the handlebar page.
index.js
app.post('/first',function(req,res){
var session = require('express-session');
req.session.fullname =Email;
res.render('first',
{Email:req.session.fullname,arrayofpromotions:arrayofpromotion,layout:false});
first.handlebars
first.handlebars
<li><a href="#"> {{#if Email}} Welcome {{ Email }}{{/if}}</a></li>
当从/first url请求页面时,我可以读取会话变量(如已渲染的那样),但不能从其他页面读取,我希望变量可用而不渲染它.这样我也可以从其他页面读取会话变量.
When the page is requested from /first url, i can read the session variable, as i have rendered it, but not from other pages, i want the variable available without rendering it. So that i can read the session variables from some other pages also.
就像我去/second一样,req.session.fullname也应该包含值.
Like if i go to /second also the req.session.fullname should contain value.
我阅读了关于stackoverflow的其他答案,但没有得到确切的解决方案.请帮忙.
I read some other answers on stackoverflow but i didnt get the exact solutions for it. Please help.
推荐答案
车把中的会话:
// app.js
app.use(session(options));
app.use(function (req, res, next) {
res.locals.session = req.session;
next();
});
在 handlebars
模板上使用
如下: {{session.SOMETHING_THAT_YOU_ASSIGNED}}
这篇关于如何在所有把手页面中获取Express Session变量,现在我每次都渲染它,我希望只完成一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!