node.js - 如何解决Nodejs中的net::ERR_ABORTED 404

标签 node.js express

我的项目目前即将结束,但我仍然收到一个错误。我似乎无法弄清楚为什么会收到错误:

"Failed to load resource: the server responded with a status of 404 (Not Found)".

下面是我的文件布局、server.js 和脚本标记。我的布局与我过去所做的所有其他项目相同,但由于某种原因,这个错误不断出现。

服务器.js

"use strict";

// DEPENDENCIES
require("dotenv").config();
const express = require("express");
const session = require("express-session");
const passport = require("passport");
const path = require("path");
const ejs = require("ejs");
const logger = require("morgan");

var createError = require("http-errors");
var cookieParser = require("cookie-parser");
const flash = require("connect-flash");
const favicon = require("serve-favicon");

// ROUTES REQUIRED
const main = require("./routes/main");
const about = require("./routes/about");
const contact = require("./routes/contact");
const profile = require("./routes/profile");
const pricing = require("./routes/pricing");
const help = require("./routes/help");
const login = require("./routes/login");
const signup = require("./routes/signup");
const forgot_password = require("./routes/forgot-password");

// PORT
const port = 3000;
const app = express();

// COOKIES AND SESSION
app.use(
  session({
    secret: process.env.SECRET,
    resave: false,
    saveUninitialized: true,
  })
);

app.use(passport.initialize());
app.use(passport.session());

// DATABASE
require("./config/database.js");

// PASSPORT AUTHENTICATION
require("./config/passport.js");

// VIEWS SETUP
app.set("views", path.join(__dirname + "/views"));
app.set("view engine", "ejs");
app.set("view cache", false);

// MIDDLEWARE
app.use(favicon(__dirname + "/public/favicon.ico"));
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use("/public", express.static(path.join(__dirname + "/public")));
app.use(flash());

// ROUTES

app.use("/", main);
app.use("/about", about);
app.use("/contact", contact);
// PRICING
app.use("/pricing", pricing);
// PROFILE
app.use("/profile", profile);
app.use("/help", help);
app.use("/login", login);
app.use("/signup", signup);
app.use("/forgot-password", forgot_password);

// Logout
app.get("/logout", function (req, res) {
  res.clearCookie("connect.sid");
  res.redirect("/");
});

app.listen(process.env.PORT || port, (err, done) => {
  if (!err) {
    console.log({ message: "success!" });
  } else {
    return err;
  }
});

home.ejs(带脚本的主页布局。)

--------------------------------------------------------------------------
    <section class="ad-analytics">
      <div class="container-fluid ad-analytics__contain">
        <div class="row ad-analytics__row">
          <div class="col-md-6" id="meetings-img-holder">
            <!-- bg-img holder -->
          </div>
          <div class="col-md-6 ad-analytics__textbox">
            <div class="col-sm-12 ad-analytics__info">
              <h1 class="h2">Analytical Precision</h1>
              <p>
                Getting ahead of the curve is the best way to scale above the
                compeititon. With our machine learning tools, you can leverage
                your data and get real insight on what your customers want from
                you.
              </p>
              <a
                class="btn btn-outline-light btn-sm"
                href="/machine-learning"
                role="button"
                >Get Started</a
              >
            </div>
          </div>
        </div>
      </div>
    </section>
    <%- include('partials/footer.ejs') %> 
    <%- include('partials/scripts.ejs') %>
  </body>
</html>
------------------------------------------------------------------------------------
(Inside partials/script.js)

<script
  src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b5b8b8a3a4a3a5b6a797e2f9e6f9e4" rel="noreferrer noopener nofollow">[email protected]</a>/dist/js/bootstrap.bundle.min.js"
  integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
  crossorigin="anonymous"
></script>
<script type="javascript" src="/server.js"></script>

文件布局

enter image description here

最佳答案

您不应该使用<script>用于导入服务器端文件的标签,例如 server.js ,该文件用于运行node服务器

假设您为主页制作了一个脚本,您需要将其保存在 /public 中并将其发送到客户端以供浏览器解释,方法是将其添加到 partials/scripts.ejs

示例

<script type="javascript" src="/home_script.js"></script>

路径是

public/home_script.js

编辑: 感觉你还是有点困惑所以我举个例子

server.js你有

const main = require("./routes/main");
app.use("/", main); 

考虑一下文件main.js就像从 server.js 获取函数一样并将其移动到单个文件

现在在main.js我猜你有这样的功能:

router.get('/', (req,res,next) => {
 res.render('home.ejs')

})

上面的代码是服务器端代码的一部分,不应发送给用户(客户端)

现在在里面home.ejs 你有你的部分,然后是脚本部分

<script type="javascript" src="/bootstrap.bundle.min.js"></script>
<script type="javascript" src="/home_script.js"></script>

此文件home_script应该包含页面到达后您想要执行的操作 user (我们称之为客户端)

举个例子:

如果你有按钮,并且你想在单击时执行某些操作,则可以在 home_script.js 中编写该部分 javascript

关于node.js - 如何解决Nodejs中的net::ERR_ABORTED 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70430548/

相关文章:

javascript - 找不到模块 : Error: Can't resolve '@mui/icons-material/Menu' in 'D:\

sql-server - 如何使用 Azure 移动应用中的表 Controller 访问 View ?

javascript - 在不解决 promise 的情况下使用 $http.post 和 res.redirect

node.js - node/express REST API 应用程序 - 将参数传递给 GET 方法

javascript - 当我有更多后端 javascript 文件时,是否应该将 node_modules 放入单独的文件夹中?

Node.js:异步函数中的尾部调用是否有优化?

javascript - 循环结束后如何发送函数结果?

node.js - Express.js - 类似 ASP.NET 的 MVC 路由

mysql - 有没有比使用 MySQL Schedule events 更好的方法来更新数据库表中的列?

javascript - 使用 passport.js 的身份验证与语法混淆?