gpt4 book ai didi

javascript - NodeJS - 将变量发送到客户端

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:06 27 4
gpt4 key购买 nike

嗨,我正在使用 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);
});
});

(部分)客户与 Jade :

body(ng-app='blogApp')
if my22
h3 hiiiiii
else
h4 2222222

你认为我应该做什么?

最佳答案

您实际上在这里谈论的是服务器端模板,而不是客户端。

如果您想让 Jade 模板可以使用值,只需传入 JavaScript 文字作为 View 函数的第二个参数即可。

let my22 = true;
reply.view('index', {my22 :my22} );

关于javascript - NodeJS - 将变量发送到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38941941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com