您好我正在使用hapi并扩展hapi-auth-cookie示例,并希望在用户登录时在客户端页面上“显示”某些内容。但是无法将我的变量发送到客户端。这是什么ID确实
服务器:
'use strict';
const Hapi = require('hapi');
const Vision = require('vision');
const Inert = require('inert');
const Path = require('path')
const internals = {};
let uuid = 1; // Use seq instead of proper unique identifiers for demo only
const rootHandler = function (request, reply) {
reply.view('index', {
title: 'examples/views/jade/index.js | Hapi ' + request.server.version,
message: 'Index - Hello World!'
});
};
let my22 = true ;
const users = {
geek: {
id: 'john',
password: 'password',
name: 'John Doe'
}
};
const login = function (request, reply) {
if (request.auth.isAuthenticated) {
return my22 ;
reply.view('index');
}
let message = '';
let account = null;
if (request.method === 'post') {
if (!request.payload.username || !request.payload.password) {
message = 'Missing username or password';
}
else {
account = users[request.payload.username];
if (!account || account.password !== request.payload.password) {
message = 'Invalid username or password';
return console.log('wrong pass');
}
}
}
if (request.method === 'get' || message) {
return reply.view('login')
};
const sid = String(++uuid);
request.server.app.cache.set(sid, { account: account }, 0, (err) => {
if (err) {
return reply(err);
}
request.cookieAuth.set({ sid: sid });
return reply.redirect('/');
});
};
const logout = function (request, reply) {
request.cookieAuth.clear();
return reply.redirect('/login');
};
const server = new Hapi.Server();
server.connection({ port: 8000 });
server.register(require('../'), (err) => {
if (err) {
throw err;
}
server.register(require('vision'), (err) => {
if (err) {
console.log("Failed to load vision.");
}
});
server.register(Inert, () => {});
server.views({
engines: { jade: require('jade') },
path: __dirname + '/templates',
compileOptions: {
pretty: true
}
});
const cache = server.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
server.app.cache = cache;
server.auth.strategy('session', 'cookie', false, {
password: 'password-should-be-32-characters',
cookie: 'sid-example',
redirectTo : '/login',
isSecure: false,
validateFunc: function (request, session, callback) {
cache.get(session.sid, (err, cached) => {
if (err) {
return callback(err, false);
}
if (!cached) {
return callback(null, false);
}
return callback(null, true, cached.account);
});
}
});
console.log(cache);
server.route(
{ method: 'GET',
path: '/',
config: {
handler: rootHandler
}
});
server.route({
method: ['GET', 'POST'],
path: '/login',
config:{
handler: login,
//auth: { mode: 'try' },
plugins: {
'hapi-auth-cookie': { redirectTo: false }
}
}
});
server.route({
method: 'GET',
path: '/logout',
config: {
handler: logout
}
});
server.route({
method: 'GET',
path: '/bower_components/{params*}',
handler: {
directory: {
path:'bower_components'
}
}
});
server.route({
method: 'GET',
path: '/public/{params*}',
handler: {
directory: {
path:'public'
}
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server is listening at ' + server.info.uri);
});
});
(部分)客户与玉:
body(ng-app='blogApp')
if my22
h3 hiiiiii
else
h4 2222222
你觉得我应该怎么做?
答案 0 :(得分:1)
你实际上是在谈论服务器端模板,而不是客户端。
如果你想让jade模板可以使用值,只需传入一个JavaScript文字作为view函数的第二个参数。
let my22 = true;
reply.view('index', {my22 :my22} );