我的项目目前即将结束,但我仍然收到一个错误。我似乎无法弄清楚为什么会收到错误:
"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>
文件布局
最佳答案
您不应该使用<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/