Jordy337
Jordy337

Reputation: 420

Node JS Server, cannot get index.html

I am making an app with socket.io, so right now I'm making the user interface with HTML and CSS, and everything is working except that when I try to use the nav to go back to the index.html it can't get it. The other Nav elements work because they are in the public directory, but if I move the index file to the public directory it will say that it can't find an index.html at all. So, my main question is, how do I get it to be able to find the HTML file, not in the public directory or to get it to find the index file in the public directory to start off with?

File Structure:

index.html
server.js
public
   settings.html
   friends.html

HTML for nav:

        <nav>
            <ul>
                <li><a href="http://localhost:5000/index.html" >Chat</a></li>
                <li><a href="http://localhost:5000/friends.html">Friends</a></li>
                <li><a href="http://localhost:5000/settings.html">Settings</a></li>
            </ul>
        </nav>

Relevant JS:

var express = require('express');
var app = express();
app.use(express.static('public'))

Upvotes: 0

Views: 2902

Answers (1)

Quentin
Quentin

Reputation: 944441

how do I get it to be able to find the html file not in the public directory

You need to either:

  • Make your static route look in the root directory instead of the public directory (not a good idea because you have server code there)
  • Set up another static route (ditto)
  • Write an explicit get handler for / which has code which reads index.html and returns it

Upvotes: 4

Related Questions