【问题标题】:nodejs Express htmlnodejs Express html
【发布时间】:2013-06-28 15:33:00
【问题描述】:

当我使用 Express 框架时,我将“.html”文件放入文件夹视图,然后将“.js”和“.css”文件公开。当我运行我的应用程序时,我可以获得 js 和 css,但是获取不到articlecontent.html。下面是我的页面index.html。

<div class="nav-collapse">
    <ul class="nav">
        <li><a href="#">Home</a></li>
        <li class="active"><a href="articlecontent.html" target="home">Content</a></li>
    </ul>
    <div id="container" class="container" style="height: inherit">
        <iframe id="home" name="home" scrolling="no">content</iframe>
    </div>
</div>

app.get("/",function index(req,res){
    res.render("index.html");
});

【问题讨论】:

  • 你有articlecontent.html的视图和路由吗?
  • 但是如果我把articlecontent.html放到视图中,并为articlecontent.html添加一个路由,它怎么知道改变iframe.code的属性“src”:Content app.get('/articlecontent',function(req,res){res.render("articlecontent.html")});

标签: html node.js express


【解决方案1】:

views 文件夹通常用于存储模板文件;换句话说,该文件夹包含您使用res.render() 发送的文件。

如果您想提供非模板 HTML 文件,例如 articlecontent.html,则应将其放在公用文件夹中,以便由 express.static 中间件提供服务(index.html 也是如此: 如果你不需要模板引擎渲染它,你也可以把它移到 public 文件夹。

【讨论】:

  • 我明白了。但是如果我将articlecontent.html放到视图中,并为articlecontent.html添加路由,它怎么知道改变iframe.code的属性“src”:内容 app.get('/articlecontent',function(req,res){res.render("articlecontent.html")});
  • @BruceWar 我不确定你的意思。
  • @BruceWar 如果您希望文件中的标记是数据驱动的,则需要使用视图引擎。 consolidate.js 的文档有一个很好的选项列表,模块本身可以帮助它们中的任何一个使用 Express。相关:app.engine().
  • @robertklep 我的意思是如果我将 articlecontent.html 放到视图中,我该怎么办?我想实现当我点击标签时,标签iframe将指向articlecontent.html的功能。
  • @BruceWar 这是你必须在客户端实现的东西,例如使用jQuery
猜你喜欢
  • 2017-01-23
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 2015-08-02
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多