简体   繁体   中英

Why is my Node.js website not finding pages in public folder?

I am quite new to web development with node.js . 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:

  • index.html
  • index.js
  • public/
    • pages/
      • terms-and-conditions.html

My index.js file looks like:

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:

<li><a href="/public/pages/terms-and-conditions.html">Terms &amp; Conditions</a></li>

I have tried a many different paths to my file but must be missing something obvious.

SOLVED:

I needed to keep the path like follows:

<li><a href="pages/terms-and-conditions.html">Terms &amp; Conditions</a></li>

The "/public/pages/terms-and-conditions.html" is indicating that this is an absolute path.

Try "./public/pages/terms-and-conditions.html".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM