freeCodeCamp Challenge Guide: Serve an HTML File

Serve an HTML File


Problem Explanation

You probably need to comment out the last challenge. If you have a website and want to serve an index.html file you probably want to put this in a public folder. This is to ensure the public doesn’t see something you dont want them to, and it sometimes is called “public” or “views,” but you can technically call it whatever you want.

To serve an index.html in a folder called “public” at the root domain you would do so like this:

app.get("/", function(req, res) {
  res.sendFile(__dirname + "/public/index.html");
});

Note: __dirname returns the root directory is a best practice for node developers.

11 Likes