简体   繁体   English

如何使用dotenv EJS文件-它可能吗?

[英]How to use dotenv EJS file - Its possible?

I'm beginner with Nodejs, so, i'm sorry if I did some wrong, and tell me how to solve, please... 我是Nodejs的初学者,所以对不起,如果我做错了什么,请告诉我如何解决,请...

In my case, I put all credentials inside .env file and my index.ejs try extract the data, but, I see error " require is not defined " because I try use ' dotenv ' here, see my example (.ejs). 就我而言,我将所有凭据放入.env文件中,而index.ejs尝试提取数据,但是,我看到错误“ require is not defined ”,因为我在这里尝试使用“ dotenv ”,请参见示例(.ejs)。

In the case, 'dotenv' is a module too extract values inside .env file and this work for give security to the values of the variables . 在这种情况下,“ dotenv”也是一个模块,也可以在.env文件中提取值,该工作可确保variablesvalues安全。

If it does not have like, I would like to know what I could do to solved the problem, remembering that it is for security of credentials. 如果没有,我想知道该如何解决该问题,并记住这是为了确保凭据的安全性。

My new EJS file: 我的新EJS文件:

    <html>
            <head>
              <script src="jquery-3.1.1.js"></script>
              <script src="app.js"></script>
              <base href="/">
              <title>XXXXXXXXXXXXXXXX</title>
              <meta name="viewport" content="width=device-width, initial-scale=1">
              <meta http-equiv="X-UA-Compatible" content="IE=edge">
              <meta property="og:image" content="XXXXXXXXXXXXXXXX.svg" />
              <meta property="og:title" content="XXXXXXXXXXXXXXXXXXSimple" />
              <meta property="og:description" content="My description" />
              <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
              <link rel="stylesheet" href="css/app.css">
            </head>
            <body>
              <div id="view-change-button" class="button" onclick="Payloadxxxxxxx.togglePanel(event, this)">
                <img class="option full" src="../img/Chat Button.png">
                <img class="option not-full" src="../img/Code Button.png">
              </div>
              <div id="contentParent" class="responsive-columns-wrapper">
                <div id="chat-column-holder" class="responsive-column content-column">
                  <div class="chat-column">
                    <div id="scrollingChat"></div>
                    <label for="textInput" class="inputOutline">
                      <input id="textInput" class="input responsive-column"
                        placeholder="Digite algo" type="text"
                        onkeydown="xxxxxxxxxxxxxxxxPanel.inputKeyDown(event, this)">
                    </label>
                  </div>
                </div>
                <div id="payload-column" class="fixed-column content-column">
                  <div id="payload-initial-message">
                    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxOtherDescription.
                  </div>
                  <div id="payload-request" class="payload"></div>
                  <div id="payload-response" class="payload"></div>
                </div>
              </div>
     <script>
     var lgn = <%- JSON.stringify(lgn) %>; //solution by @Jak
     var pas = <%- JSON.stringify(pas) %>; //but show the values in console
     //   var lgn = process.env.LOGIN;
     //   var pas = process.env.PASS;
        var numberOrigin = 330;

function test(){
  var obj2 = login(lgn, pas, numberOrigin);
  numberOrigin++;
}

    var obj;

    function xxxx(xxxxxxxxxxxxxxxxxxxx){
      numberOrigin +=1;
     //some codes with ajax


    function otherFunction(){
      //otherFunction code    
    }
   </script>
       // I need the .js files for my function to work fine
      <script src="js/xxxxx.js"></script>
      <script src="js/xxxxxxxx.js"></script>
      <script src="js/xxxxxxxxxx.js"></script>
      <script src="js/xxxxxxxxxx.js"></script>
      <script src="js/xxxxxxxxx.js"></script>
    </body>
    </html>

Obs.: My app.js works fine with require( 'dotenv' ).config( {silent: true} ); 实验值:我app.js 正常工作require( 'dotenv' ).config( {silent: true} ); , ( .env file with credentials for other API), but the file is .js and not .ejs . ,(具有其他API凭据的.env文件),但该文件是.js而不是.ejs

I'm not sure if dotenv works with EJS , but, if not, someone can help me how to ensure that my credentials will not be seen? 我不确定dotenv可以与EJS ,但是,如果没有,有人可以帮助我如何确保不会看到我的凭据

EDIT: After try with the possible solution By @Jak Hammond, my code index.ejs : 编辑:通过@Jak Hammond尝试可能的解决方案后,我的代码为index.ejs

在此处输入图片说明

My code inside Console Chrome (It's possible see the credentials): 我在控制台Chrome中的代码(可能会看到凭据):

在此处输入图片说明

If I've understood your question correctly you want to pass some data into the EJS view you're rendering? 如果我正确理解了您的问题,是否要将一些数据传递到您要呈现的EJS视图中? If so, then this question might be of use; 如果是这样,那么这个问题可能有用。 Pass variables to JavaScript in ExpressJS 将变量传递给ExpressJS中的JavaScript

You have to load the data on the server and then pass it down inside an object which you can then reference inside your view. 您必须将数据加载到服务器上,然后将其传递到一个对象中,然后可以在视图中引用该对象。

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

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