简体   繁体   中英

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...

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).

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 .

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:

    <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} ); , ( .env file with credentials for other API), but the file is .js and not .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?

EDIT: After try with the possible solution By @Jak Hammond, my code index.ejs :

在此处输入图片说明

My code inside Console Chrome (It's possible see the credentials):

在此处输入图片说明

If I've understood your question correctly you want to pass some data into the EJS view you're rendering? If so, then this question might be of use; Pass variables to JavaScript in ExpressJS

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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