簡體   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