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.
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;
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