Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS-向客户端发送变量_Javascript_Node.js_Cookies_Hapijs - Fatal编程技术网

Javascript NodeJS-向客户端发送变量

Javascript NodeJS-向客户端发送变量,javascript,node.js,cookies,hapijs,Javascript,Node.js,Cookies,Hapijs,您好,我正在使用hapi并扩展示例,希望在用户登录时在客户端页面上“显示”一些内容。但无法将我的变量发送到客户端。以下是id所做的操作 服务器: 'use strict'; const Hapi = require('hapi'); const Vision = require('vision'); const Inert = require('inert'); const Path = require('path') const internals = {}; let uuid = 1;

您好,我正在使用hapi并扩展示例,希望在用户登录时在客户端页面上“显示”一些内容。但无法将我的变量发送到客户端。以下是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

您认为我应该怎么做?

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

如果要使值可用于jade模板,只需传入一个JavaScript文本作为view函数的第二个参数

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

那么在这种情况下,我应该像这样发送它?reply.view('index',{my22:true});如果你有一个变量在一个作用域中声明并可用,你可以像我做的那样。如果你所说的范围是“angular$scope”,那么你也可以做你所说的{my22:true}。不,我没有。添加了我的新行,但它不起作用。可能是因为你提到的范围,我只是指一个JavaScript范围。看我修改过的答案。是的,如果你看代码,我声明了它,但不起作用。我甚至不能在这个“如果”下安慰你