[英]Why is my Node.js website not finding pages in public folder?
I am quite new to web development with node.js
.我对使用
node.js
进行 web 开发非常陌生。 I have an issue where loading one of my pages is giving the following error in my browser:我有一个问题,在我的浏览器中加载我的一个页面时出现以下错误:
Cannot GET /public/pages/terms-and-conditions.html
The file structure looks like the following:文件结构如下所示:
My index.js file looks like:我的 index.js 文件如下所示:
const express = require('express');
var path = require('path')
const app = new express();
var port = process.env.PORT || 3000;
app.get('/', function(request, response){
response.sendFile('./index.html',{ root: __dirname });
});
app.use(express.static(path.join(__dirname, 'public')));
app.listen(port, () => console.log(`listening on port 3000!`));
My button that directs them to terms-and-conditions
is located in the index.html
file and does the following:我将他们引导至
terms-and-conditions
的按钮位于index.html
文件中,并执行以下操作:
<li><a href="/public/pages/terms-and-conditions.html">Terms & Conditions</a></li>
I have tried a many different paths to my file but must be missing something obvious.我已经尝试了许多不同的文件路径,但肯定遗漏了一些明显的东西。
I needed to keep the path like follows:我需要保持如下路径:
<li><a href="pages/terms-and-conditions.html">Terms & Conditions</a></li>
The "/public/pages/terms-and-conditions.html" is indicating that this is an absolute path. “/public/pages/terms-and-conditions.html”表示这是一个绝对路径。
Try "./public/pages/terms-and-conditions.html".试试“./public/pages/terms-and-conditions.html”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.