freeCodeCamp/guide/english/certifications/apis-and-microservices/basic-node-and-express/serve-an-html-file/index.md

1.0 KiB

title
Serve an HTML File

Serve an HTML File

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.

Help our community expand these hints and guides.