This is the code I have so far for my router. Just a basic GET request to the root of localhost:3000.

function fileSending(req, res) {
res.sendFile(__dirname + "/views/index.html");
};

app.get("/", fileSending);

I have also tried importing path and using the path.join method:

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

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

module.exports = app;

update: Have also tried suggestions from another post. Still not seeming to render my html page on localhost. Getting this error:

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

My file structure relevant to this is as follows: |-- views | -- index.html |myApp.js (express route)

I have also tried:

app.get("/", (req, res) => {
  res.sendFile("views/index.html", { root: __dirname })
});

And:

app.get("/", (req,res) => {
  res.sendFile(path.resolve("/views/index.html"))
});

1

There are 1 best solutions below

0
Ajasa Taiwo On

looking at this error i once add it when i was learning Backend Development and Apis on freeCodeCamp that time i was using replit so i just deleted the repl and used the replit starter project specified in the tutorial and i used this code

let express = require("express");
let app = express();
const absolutePath = __dirname + "/views/index.html";
app.get("/", (req, res) => {
    res.sendFile(absolutePath);
});
module.exports = app;