Index.html is not loaded in the browser

I am new to Node and Express and I am stuck on the basic code.

This is my code

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

const app = express();
const router = express.Router();

let port = process.env.port || 3000;


router.get("/", (req, res)=>{
   console.log("hello world");
   res.sendFile('index.html', {root: path.join( __dirname, '/public/')})


app.listen(port, (err)=> {
        return(console.log("i am listening"));
    console.log(`listening to the port ${port}`);

This is my project’s directory structure:

enter image description here

When I remove the option in the express.static( ), it opens but the public has index.html but I want to open with res.sendFile( ) because I am learning.

Thank you in advance !!!

115 thoughts on “Index.html is not loaded in the browser”

Leave a Comment