简体   繁体   English

如何在Sails项目的ejs视图中访问nodeJS变量?

[英]How can i access a nodeJS variable in ejs views in a sails project?

My LoginController.js looks like this: 我的LoginController.js看起来像这样:

module.exports = {
  getAuthorizationLink: function (req, res) {
    res.send({
      authorization_link: sails.config.FACEBOOK_LOGIN_URL
    })
  }
}

I need to redirect to the authorization_link when a button is clicked 单击按钮时,我需要重定向到authorization_link

<div class="col-md-4 text-center">
  <button id="authenticate" class="btn btn-primary">Authenticate   Page</button>
  <script type="text/javascript">
    document.getElementById("authenticate").onclick = function () {
     ...
    };
  </script>
</div>

Here you are looking to mix server-side (EJS) & client-side JS code. 在这里,您要混合使用服务器端(EJS)和客户端JS代码。

It is possible, makes sense to do sometimes but it is not clean. 有时可能有道理,但并不干净。 Once you understand you are doing this. 一旦了解,您就可以执行此操作。 Variable can be passed and accessed. 可以传递和访问变量。

Using EJS, write JS code for client side eg 使用EJS,为客户端编写JS代码,例如

var auth_link = '<%= authorization_link %>';

this line will become something like below for client-side JS 对于客户端JS,此行将变为以下内容

var auth_link = 'https://fb.com/login';

Now you can use auth_link in client-side JS as required 现在,您可以根据auth_link在客户端JS中使用auth_link

Also, check res.view for responding with HTML page 另外,请检查res.view以响应HTML页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM