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

时间:2017-01-11 13:01:56

标签: javascript node.js ejs

我是Nodejs的初学者,所以,如果我做错了,我很抱歉,请告诉我如何解决,请...

在我的情况下,我将所有凭据放在.env文件中,而我的index.ejs尝试提取数据,但是,我看到错误“require is not defined”因为我尝试使用'{{1在这里,看看我的例子(.ejs)。

在这种情况下,'dotenv'是一个模块,也提取dotenv文件中的值,这项工作可以为.env的{​​{1}}提供安全保障。

如果没有,我想知道我能做些什么来解决这个问题,记住它是为了证券的安全性。

我的新EJS文件:

values

视频:我的app.js 正常variables,( <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> 文件包含其他API的凭据),但该文件为require( 'dotenv' ).config( {silent: true} );,不是.env

我不确定.js是否适用于.ejs,但是,如果没有,有人可以帮助我如何确保不会看到我的凭据?< / p>

修改 尝试使用@Jak Hammond可能的解决方案后,我的代码dotenv

enter image description here

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

enter image description here

1 个答案:

答案 0 :(得分:1)

如果我已正确理解您的问题,您想将一些数据传递到您正在渲染的EJS视图中吗?如果是这样,那么这个问题可能会有用; Pass variables to JavaScript in ExpressJS

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