【问题标题】:NodeJS - sending Variable to clientNodeJS - 向客户端发送变量
【发布时间】:2016-12-20 21:11:47
【问题描述】:

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

你觉得我应该怎么做?

【问题讨论】:

    标签: javascript node.js cookies hapijs


    【解决方案1】:

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

    如果你想让值对翡翠模板可用,只需传入一个 JavaScript 文字作为视图函数的第二个参数。

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

    【讨论】:

    • 所以在这种情况下我应该这样发送? reply.view('index', {my22 :true} );
    • 好吧,如果你声明了一个变量并且在作用域中可用,你可以做我所做的。你也可以做你展示的事情 {my22: true}
    • 如果你所说的范围是指“角度 $scope”,不,我没有。添加了我的新行,但它不起作用。可能是因为你提到的范围
    • 我的意思是一个 JavaScript 范围。请参阅我修改后的答案。
    • 是的,如果你查看代码,我就声明了它,但它不起作用。我什至无法在这个“IF”中控制台
    猜你喜欢
    • 1970-01-01
    • 2014-08-17
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多