Basic Node and Express - Serve an HTML File

Tell us what’s happening:
I am trying to send an absolute file path as a response to a GET request made to the root “/” directory. When I navigate over to localhost:3000, the page seems to not load my html page and displays an error.

Here is what I have tried so far:

app.get("/",  (req, res) => {
    res.sendFile(__dirname + "/views/index.html")

also have tried importing the path module to use path methods accordingly:

app.get("/", (req, res) => {
   res.sendFile(path.join(__dirname, "views", "index.html"))

the error I am receiving when loading localhost:3000 is as follows:

ENOENT: no such file or directory, stat ‘/Users/loganaaron/Desktop/FreeCodeCamp/boilerplate-express/views/index’

is that all of your code?

I have the basic setup for an express application in myApp.js:

let express = require("express");
let app = express();
const path = require("path");

function helloExpress(req, res) {
  res.send("Hello Express");

function fileSending(req, res) {
  // __dirname returns the root directory(best practice for node developers)
  res.sendFile(__dirname + "/views/index.html"); // will respond to a request made to "/" with a file.

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname,  "views", "index"));

module.exports = app;

are you in a free code camp challenge? if so could you give us a link to the challenge

1 Like

Yes I am in a challenge, here is the link.

also, I have tried moving the index.html file into the public folder, although having the index module in the views folder should work just fine

I got it figured out. I had commented out code right above it, Removed the code, and it works as intended. Dont know as to why comments alter with the get middleware, but it works lol. Thanks for the help @jasper2

your welcome, always like to help