繁体   English   中英

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

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

我是Nodejs的初学者,所以对不起,如果我做错了什么,请告诉我如何解决,请...

就我而言,我将所有凭据放入.env文件中,而index.ejs尝试提取数据,但是,我看到错误“ require is not defined ”,因为我在这里尝试使用“ dotenv ”,请参见示例(.ejs)。

在这种情况下,“ dotenv”也是一个模块,也可以在.env文件中提取值,该工作可确保variablesvalues安全。

如果没有,我想知道该如何解决该问题,并记住这是为了确保凭据的安全性。

我的新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>

实验值:我app.js 正常工作require( 'dotenv' ).config( {silent: true} ); ,(具有其他API凭据的.env文件),但该文件是.js而不是.ejs

我不确定dotenv可以与EJS ,但是,如果没有,有人可以帮助我如何确保不会看到我的凭据

编辑:通过@Jak Hammond尝试可能的解决方案后,我的代码为index.ejs

在此处输入图片说明

我在控制台Chrome中的代码(可能会看到凭据):

在此处输入图片说明

如果我正确理解了您的问题,是否要将一些数据传递到您要呈现的EJS视图中? 如果是这样,那么这个问题可能有用。 将变量传递给ExpressJS中的JavaScript

您必须将数据加载到服务器上,然后将其传递到一个对象中,然后可以在视图中引用该对象。

暂无
暂无

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

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