bootstrappedUser - 将Jade或EJS改为HTML [英] bootstrappedUser - Jade or EJS to HTML

查看:141
本文介绍了bootstrappedUser - 将Jade或EJS改为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mean stack上做了一些指导,然后到了目前我被困住的地步。
按照本指南,我创建了一个简单的身份验证,可以使用Passport JS登录。无论如何,每次页面刷新时,身份验证都会重新启动(客户端无法识别它)。



由于这是假设在指南中发生的,修复它,指导如下说。
1.创建一个Jade文件并插入:

  if !! bootstrappedUser 
脚本。
window.bootstrappedUserObject =!{JSON.stringify(bootstrappedUser)}

这在我的HTML文件,但它不起作用:

 < script type ='text / javascript'> 

if(bootstrappedUser!=undefined){
window.bootstrappedUserObject = JSON.stringify(bootstrappedUser);
}

< / script>

获取错误:未捕获的ReferenceError:未定义bootstrappedUser
,即使我已经在后端js文件中创建了变量并将它分配给了req.user。



我想这个文件包含在我的主布局(index.html)中。问题是我没有使用Jade作为模板引擎,而是纯HTML,我不知道如何将这些代码转换为我的index.html中的简单HTML。



它接到这个声明只在用户点击登录按钮时初始化。有没有人有任何想法或解决方案如何以纯HTML编写上述代码。



我浏览了StackOverflow并发现了几乎相似的问题,但不够类似。
在此先感谢,
Aleksandar

解决方案

bootstrappedUser 是一个由服务器端代码传递给Jade编译器的变量,它在编译时将其注入到HTML中。如果你打算自己写HTML,你不能从服务器端代码注入变量,显然是因为HTML只是一个静态标记。您可能必须通过ajax或其他东西从服务器端获取客户端的变量。


通过Angular控制器可以说?


您可以首先在服务器上定义一个服务器,用于提供您想要的变量



($ req.user)
res.json(req.user) ;
else
res.status(401).end();
});

然后在客户端角度上,您可以执行一个http请求并获取该变量

  $ http.get('/ bootstrappedUser')
.success(函数(数据,状态,头文件,配置){
$ scope.bootstrappedUser = data;
});


I've been doing some guide on Mean stack and I came to a point where I'm currently stuck. Following this guide, I have created a simple authentication where I'm able to log in using Passport JS. Whatsoever, each time when page refreshes, the authentication gets restarted (client doesn't recognise it anymore).

Since this is suppose to happen in the guide and we are about to fix it, guide said the following. 1. Create a Jade file and insert this:

    if !!bootstrappedUser
  script.
    window.bootstrappedUserObject = !{JSON.stringify(bootstrappedUser)}

I've tried this in my html file but it doesn't work:

  <script type='text/javascript'>

          if (bootstrappedUser != "undefined" ){
          window.bootstrappedUserObject = JSON.stringify(bootstrappedUser);
           }

    </script>

Getting the error: Uncaught ReferenceError: bootstrappedUser is not defined even though I have created the variable in my backend js file and assigned req.user to it.

I'm suppose to have this file included in my main layout (index.html). The problem is that I'm not using Jade as template engine but plain HTML and I don't know how to transform this code up there to work as simple HTML in my index.html.

It seams that this statement up here only initialise when user hits the login button. Does anyone have any idea or solution how to write the above code in plain HTML.

I browsed StackOverflow and found almost similar problems but not similar enough. Thanks in advance, Aleksandar

解决方案

bootstrappedUser is a variable passed by server-side code to Jade compiler which injects it into the HTML while compiling. If you plan on writing the HTML yourself you can not inject variables from server-side code, obviously because HTML is just a static markup. You'll probably have to get that variable at the client-side from the server yourself via ajax or something.

Via Angular controller let's say ?

You can first define a route on server that serves the variable you want

app.get('/bootstrappedUser', function(req, res){
    if(req.user)
        res.json(req.user);
    else
        res.status(401).end();
});

Then on client-side angular you can perform an http request and get that variable

$http.get('/bootstrappedUser')
    .success(function(data, status, headers, config) {
        $scope.bootstrappedUser = data;
    });

这篇关于bootstrappedUser - 将Jade或EJS改为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆