html - 无法在 Node.js 中获取 html 页面

标签 html node.js express

在这种情况下,我能够显示主页index.html,但我想要像链接一样打开的页面不会显示。这是我显示 index.html 时的代码:

app.get('/', function(req, res){
     res.sendFile(path.join(__dirname+'/public/index.html'));
 });

index.html 的正文:

<body>
  <div style="margin:100px;">
    <nav class="navbar navbar-inverse navbar-static-top">
  <div class="container">
    <a class="navbar-brand" href="/">Express HTML</a>
    <ul class="nav navbar-nav">
      <li class="active">
        <a href="/">Home</a>
      </li>
      <li>
        <a href="../app/public/signup.html">Signup</a>
      </li>
      <li>
        <a href="../app/public/login.html">Login</a>
      </li>
    </ul>
  </div>
</nav>
  </div>
</body>

一个问题是我尝试了很多方法来显示singup.html和login.html 它不起作用。 谁有解决办法吗?谢谢!

最佳答案

您使用的path.join略有不正确; path.join 采用三个参数,而不是单个“预连接”参数。

res.sendFile(path.join(__dirname+'/public/index.html'));

应该是:

res.sendFile(path.join(__dirname, '/public', 'index.html'));

或者,您可以完全跳过连接:

res.sendFile( __dirname + "/public/" + "index.html" );

希望这有帮助!

关于html - 无法在 Node.js 中获取 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885517/

相关文章:

linux - 如何在 MacOS 和 Linux 上的 Node 中启动子进程

javascript - 将node.js应用程序部署到elastic beanstalk(使用express)

javascript - react 应用程序 : Start API server with React server

javascript - 不同浏览器对相同字体的不同显示

jQuery给定输入ID,获取标签文本

javascript - js导入找不到wasm文件

javascript - NodeJS 异步/等待顺序不起作用

http - Express.js HTTP 请求超时

javascript - 是否可以从嵌入式YouTube iframe获取<video>元素?

asp.net - 为什么 DIV 在此代码中的 ASP 标记内无效?