【问题标题】:Cannot GET /to-do.html when I click on button linked (Node js)当我单击链接的按钮时无法获取 /to-do.html (Node js)
【发布时间】:2021-12-31 11:59:52
【问题描述】:

我正在创建 nodeJS 应用程序。我有 index.html,其中链接了其他 HTML 文件(notes.html、to-do.html 等),但这些文件不起作用:

无法获取 /to-do.html

我的其他 HTML 文件现在只是带有少量文本的空白文件,但我将对其进行编辑。主要问题是我无法加载这些页面,我现在该怎么办。

index.html 文件:

<nav class="navbar navbar-expand-lg navbar-dark">
    <div class="collapse navbar-collapse" id="nav-dropdown">
      <ul class="navbar-nav ms-right">
        <li class="nav-item">
          <a href="/to-do.html" class="nav-link">To-Do</a>
        </li>
      </ul>
    </div>
</nav>

 

我的 node.js 文件

 //jshint esversion: 6

const express = require("express");
const path=require('path');
const https = require('https');

const app = express();

app.use(express.static(path.join(__dirname,"/public")));

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

app.get("/toDo", function (req, res) {
    res.sendFile(__dirname + "/to-do.html");
});

app.get("/notes", function (req, res) {
    res.sendFile(__dirname + "/notes.html");
});

app.get("/currentAffairs", function (req, res) {
    res.sendFile(__dirname + "/currentAffairs.html");
});

app.listen(3000,function(){
    console.log("Server started on port 3000");
});

Code File screenshot

【问题讨论】:

    标签: html node.js express web web-development-server


    【解决方案1】:

    看起来你的链接在

    <!-- ... -->
    <a href="/to-do.html" class="nav-link">To-Do</a>
    <!-- ... -->
    

    没有链接到对应的路由

    // ...
    app.get("/toDo", function (req, res) {
        res.sendFile(__dirname + "/to-do.html");
    });
    

    试试

    <!-- ... -->
    <a href="/toDo" class="nav-link">To-Do</a>
    <!-- ... -->
    

    改为

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      相关资源
      最近更新 更多