【问题标题】:nodejs read config file in html filenodejs读取html文件中的配置文件
【发布时间】:2018-06-18 06:09:41
【问题描述】:

我有一个配置文件,其中保存了所有配置。

app: {
   root: 'rooturl'
}

我有一个基本上是模板的 html 文件。

现在我想在 html 中读取该配置文件

<img src='rooturl'>

如何从 html 文件中的配置中读取根目录

我尝试谷歌搜索,但没有找到任何东西

请指教 谢谢

【问题讨论】:

  • 你使用 express.js 吗?
  • 是的,我用express,我试过用require,但不知道怎么给src
  • 您在项目中使用的是哪个模板引擎?
  • 没有模板引擎,它是一个api代码库,我需要将html文件发送到电子邮件
  • 也许你应该使用模板引擎请检查stackoverflow.com/questions/37991995/…如果你不使用模板引擎也许你应该先阅读html文件内容然后替换字符串。

标签: html node.js express configuration


【解决方案1】:

尝试以这种方式实现它,制作如下所示的三个文件并运行它。我使用了一点 jQuery,它在 DOM 操作中非常有用。

index.html

<!DOCTYPE html>
    <html>
	    <head>
		    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		    <script type="text/javascript" src="config.js"></script>
		    <script type="text/javascript" src="add.js"></script>
	    </head>
        <body>

	        <img alt="Pulpit Rock" width="284" height="213"><br>

	        <button>Set the width attribute of the image</button>

       </body>
    </html>

在下面的文件中,我使用了 jQuery 来设置 &lt;img&gt; 的 src 属性。

添加.js

$(document).ready(function(){
    $("button").click(function(){

        $("img").attr("src", app.root);

    });
});

config.js

app = { 
    root: "https://www.w3schools.com/jquery/img_pulpitrock.jpg" 
}

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 2021-09-28
    • 2011-09-24
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2015-05-04
    相关资源
    最近更新 更多